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
 General SQL Server Forums
 New to SQL Server Programming
 datetime query

Author  Topic 

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-05-20 : 15:52:15
Hi,
Whats the best way to write a query using SQL Server 2008 for following?

I need to get records from a table whose datetime > 20-May-2012 11:22:23 but < 25-May-2010 14:55:21

Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-20 : 15:55:39
...
WHERE DateTimeColumn > '20-May-2012 11:22:23' AND DateTimeColumn < '25-May-2010 14:55:21'

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

Subscribe to my blog
Go to Top of Page

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-05-20 : 15:57:43
Hi,
Thanks for quick reply. If my production server is in Central Time Zone, it'll store datetime in the server's timezone. Now, my question is that if I want to get the local time (assuming I'm in PST timezone), how do i do that using query?

Thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-20 : 16:03:07
You can use the DATEADD function to subtract off 2 hours.

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

Subscribe to my blog
Go to Top of Page

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-05-20 : 16:04:28
Sorry, I should be more clear....Actulally customer may be in different timezones & I need to programatically calculate the local time. Can we do that using query?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-20 : 16:07:28
You can search the forums for my timezone converter function (udf_timezone_conversion). The one posted on this site does not have the fix for when the DST rules were changed a while ago, but it's a start at least. My function was corrected for the new DST rules, however I'm not the author of that so I can't post it.

We use GMT on all of our servers since we support customers in multiple timezones. GMT works best in a situation like this since the offset is 0.

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

Subscribe to my blog
Go to Top of Page

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-05-20 : 17:00:48
Hi Tara,
When you recommend GMT time for server, are you referring to "GMT - Dublin, Edinburgh, Lisbon & London" or something different, since there are several options.

Thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-20 : 17:01:42
Yes that one.

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 -