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 2005 Forums
 Other SQL Server Topics (2005)
 Help me about datetime query

Author  Topic 

tuan83vnn
Starting Member

4 Posts

Posted - 2012-03-14 : 22:56:05
Hi,
My database have two fields:startdate,stopdate
exp:
startdate : 2012-01-02 20:15:11.123
stopdate: 2012-01-03 20:15:11.123

I used datediff to get the second between two fields:
datediff(milisecond,startdate,stopdate)/1000

but result = NULL

If startdate and stopdate have the same date, query return the true result. And if they have different date, result is null.

Please help, thanks




TuanTran

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-14 : 22:59:25
[code]datediff(millisecond,startdate,stopdate)/1000[/code]

or divide by 1000.0 if you want it in decimal

Do note that you will get overflow if the different in 2 dates is huge. If you do not required that accuracy, just find the different in terms of seconds. The date different in Second will also hit that overflow problem, it is just the range is larger.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

tuan83vnn
Starting Member

4 Posts

Posted - 2012-03-14 : 23:26:47
The result is true
Thanks khtan so much,

TuanTran
Go to Top of Page
   

- Advertisement -