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 2000 Forums
 SQL Server Development (2000)
 datediff doubts

Author  Topic 

spock
Starting Member

35 Posts

Posted - 2002-01-06 : 02:22:28
Hi,
when i run the query below
select datediff( dd, cast(1-1-2002 as datetime) , getdate() )
i get the result as
39262 2002-01-06 07:11:18.263

why am i not getting 5 as the difference?


thanks
kaushik




Edited by - spock on 01/06/2002 02:34:04

Edited by - spock on 01/06/2002 02:34:40

aclarke
Posting Yak Master

133 Posts

Posted - 2002-01-06 : 02:39:33

select datediff( dd, cast('1-1-2002' as datetime) , getdate() )


Notice the apostrophes. I'll leave it to you to figure out how this changes the meaning of your code.

Go to Top of Page

Nazim
A custom title

1408 Posts

Posted - 2002-01-06 : 02:39:45
You need to put the datatime value in quotes , otherwise it will be treated as a number and an equivalent datetime is converted.

for cast(1-1-2002 as datetime)
the date it gives is '1894-07-09 00:00:00.000'

for cast('1-1-2002' as datetime) will give you 1-1-2002 only.

Remember internally Dates are stored as numbers.

Therefore , your query should be

select datediff( dd, cast('1-1-2002' as datetime) , getdate() ) this will give you 5

HTH




----------------------------
Anything that Doesn't Kills you Makes you Stronger
Go to Top of Page

Nazim
A custom title

1408 Posts

Posted - 2002-01-06 : 02:41:53
Beaten by 12 Secs

Clarke , Am sorry didnt kept anything to figure out, ofcourse if am right there


----------------------------
Anything that Doesn't Kills you Makes you Stronger

Edited by - Nazim on 01/06/2002 02:42:41
Go to Top of Page
   

- Advertisement -