Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 The conversion of a varchar data type to a datetim

Author  Topic 

abhinav864
Starting Member

4 Posts

Posted - 2010-09-15 : 17:31:22
Hi
I have two colums which are of varchar(25) and have values in them as 20081121 which mean 2008 is the year 11 is the month and 21 is date .I want to Subtract 2 similar column .

Eg: STAT1DT STAT2DT
20081121 20081202

The answer should be 9 days instead of something >9.

I tried this code
select DATEDIFF (dd ,convert(datetime,STAT1DT,101) ,convert(datetime,STAT2DT,101) )from NCT_Master .

But I am getting the error
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.




If some one can help me in this i would really appreciate it
Thank you.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-15 : 17:46:41
Where do you get 9 from? It should be 11.

There is no need to do those conversions:

declare @d1 varchar(25), @d2 varchar(25)

select @d1 = '20081121', @d2 = '20081202'

select datediff(dd, cast(@d1 as datetime), cast(@d2 as datetime))

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

abhinav864
Starting Member

4 Posts

Posted - 2010-09-15 : 18:49:59
The two dates i have shown is just an example.....the two columns STAT1DT and STAT2DT have 40000 rows in them .I need a code which subtracts the two colums STAT1DT and STAT2DT.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-15 : 18:55:00
Understood, so apply my sample to your table (it's just a quick copy/paste thing):

select datediff(dd, cast(STAT1DT as datetime), cast(STAT2DT as datetime))
from YourTable

And consider using proper data types going forward.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

abhinav864
Starting Member

4 Posts

Posted - 2010-09-15 : 19:09:54
@ tkizer

When i perform the above code it shows this error

Must declare the scalar variable "@STAT1DT"
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-15 : 19:13:29
Why do you have @ in your query? Use your object names, like in my last post.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

abhinav864
Starting Member

4 Posts

Posted - 2010-09-15 : 19:24:44
Sry for the trouble but executed this
select datediff(dd, cast(STAT1DT as datetime), cast(STAT2DT as datetime))from NCT_Master

the error i get is
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-15 : 19:29:18
You've got data that can't be converted to datetime data type. You will need to correct your data in order to use date/time functions.

Try the IsDate function to identify the problem values.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -