views:

25

answers:

2

Hi Everyone,

I was wondering if anyone could help me understand why these two criteria do not return the same result sets. To me, it seems weird that SQL Server 2008 R2 wouldn't know to use the offset while constraining the data. Is there a better way to do this? As far as I can tell, Criteria Two is the only way to get the correct data.

-- Criteria One
OriginationDateTimeOffset >= TODATETIMEOFFSET('2010-10-20', '-08:00') AND
OriginationDateTimeOffset < TODATETIMEOFFSET('2010-10-21', '-08:00')

-- Criteria Two
SWITCHOFFSET(OriginationDateTimeOffset, '-08:00') >= TODATETIMEOFFSET('2010-10-20', '-08:00') AND
SWITCHOFFSET(OriginationDateTimeOffset, '-08:00') < TODATETIMEOFFSET('2010-10-21', '-08:00')
+1  A: 

Why would it return the same? In criteria 1 you are comparing the original time to the offset, where in criteria 2 you are changing the offset on both.

Dustin Laine
But why should i have to switch the offset? It already has an offset tied to it, so in my opinion, i should only have to set the offset of the criteria I am passing it and it should do the rest. It seems to me like SQL Server should switch everything to 0 offset before performing the query. Why would I need to use switchoffset to put the offset into the same timezone as my other offset. Seems redundant and unnecessary.
Christopher Haws
A: 

TODATETIMEOFFSET converts the value(s) to datetimeoffset. SWITCHOFFSET changes the value to another datetimeoffset. For example:

DECLARE @OriginationDateTimeOffset DATETIMEOFFSET = '2010-10-20'

SELECT @OriginationDateTimeOffset, 
    SWITCHOFFSET(@OriginationDateTimeOffset, '-08:00'),
    TODATETIMEOFFSET(@OriginationDateTimeOffset, '-08:00')

produces:

2010-10-20 00:00:00.0000000 +00:00
2010-10-19 16:00:00.0000000 -08:00
2010-10-20 00:00:00.0000000 -08:00
bobs
Yes, i do know the difference between these. I am trying to get all records that happened within 1 PST day which is why I am giving it today and tomorrow in PST as criteria. The problem is, shouldnt SQL Server know that both of the values its comparing are DateTimeOffset's and convert them to UTC (0 offset) or something of that nature on its own? Why should I have to make sure they are in the same timezone if there is timezone information tied to both pieces of data?
Christopher Haws