SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 UTC DateTime in query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

figmo
Starting Member

18 Posts

Posted - 07/08/2012 :  23:43:59  Show Profile  Reply with Quote
I am using an orders table that has a DateTime column that represents the UTC DateTime an order was created.

I've run into a problem that seems like there should be a simple solution for - but haven't been able to find it.

Given this simple query:
SELECT * FROM ORDERS WHERE OrderDate >= @StartDate AND OrderDate <= @EndDate

Works as expected - EXCEPT for orders placed later in the day on the EndDate. Because the UTC DateTime stored is tomorrow (from my timezone)

Certainly there has to be some way to tell SQL Server that we want all orders placed on or before today - IN MY TIME ZONE?

This is part of a shopping cart software so I had no hand in the design. I could add columns - I am the administrator - but want to avoid this to make upgrades easier.

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 07/09/2012 :  00:29:39  Show Profile  Reply with Quote
SELECT * FROM ORDERS WHERE OrderDate >= DATEADD(ss,-1 * DATEDIFF(ss,GETDATE(),GETUTCDATE()),@StartDate) AND OrderDate <= DATEADD(ss,-1 * DATEDIFF(ss,GETDATE(),GETUTCDATE()),@EndDate)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

figmo
Starting Member

18 Posts

Posted - 07/09/2012 :  11:27:41  Show Profile  Reply with Quote
Thank you visakh. Your query did not work as posted - but you gave me the tools I needed. I was able to figure it out from there.

For others who may come across this thread: I had to remove the '-1 *' from his query. Like this:

SELECT * FROM ORDERS WHERE OrderDate >= DATEADD(ss,DATEDIFF(ss,GETDATE(),GETUTCDATE()),@StartDate) AND OrderDate <= DATEADD(ss,DATEDIFF(ss,GETDATE(),GETUTCDATE()),@EndDate)

Because we want to add the offset to the search date - not subtract it. The DATEDIFF as you have it will return an inverted UTC offset (postive number in the west, negative number in the east) which means we must ADD it to the start/end date - not subtract it.

Anyway - query works fine now. Thanks for your help!

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 07/09/2012 :  11:33:37  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000