2

我想知道是否有人可以帮助我理解为什么这两个标准不返回相同的结果集。对我来说,SQL Server 2008 R2 在约束数据时不知道使用偏移量似乎很奇怪。有一个更好的方法吗?据我所知,标准二是获得正确数据的唯一方法。

-- 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')
4

2 回答 2

1

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.

于 2010-10-21T23:04:48.573 回答
0

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
于 2010-10-21T23:13:07.183 回答