开发者

Why is [date] + ([time] - [offset]) non-deterministic in SQL Server 2008?

开发者 https://www.devze.com 2023-01-03 21:42 出处:网络
I\'m trying to do the following for my IIS logs table: ALTER TABLE [W3CLog] ADD [LogTime] AS [date] + ([time] - \'1900-01-01\') PERSISTED

I'm trying to do the following for my IIS logs table:

ALTER TABLE [W3CLog]
ADD [LogTime] AS [date] + ([time] - '1900-01-01') PERSISTED

However, SQL Server 2008 tells me:

Computed column 'LogTime' in table 'W3CLog' cannot be persisted because the column is non-deterministic.

The table has this definition:

CREATE TABLE [dbo].[W3CLog](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    ...
    [date] [datetime] NULL,
    [time] [datetime] NULL,
    ...
)

Why is that non-deterministic?

I really need to index that field. The table currently has 1598170 rows, and it is a pa开发者_高级运维in to query if we can't do an index seek on the full time. Since this is being UNION'd with some other log formats, we can't very easily just use the two columns separately.


Your '1900-01-01' is non-deterministic because it depends on language settings. of course, this is unambiguous for DMY or MDY settings bit generally it's ambiguous

Try '19000101': SQL Server treats dates and times somewhat oddly: "yyyy-mm-dd" can be treated as "yyyy-dd-mm" if you have British settings despite being ISO in theory

Edit: use this to remove the date aspect: DATEADD(day, 0, DATEDIFF(day, 0, [time]))

Edit2: 1st Jan 1900 is zero in the datetime formats, so no need to subtract it. Can you post sample data and output please?


Alright, I figured it out, thanks to @gbn's answer.

The following three are equivalent:

SELECT TOP 100
    [date] + ([time] - '19000101'),
    [date] + ([time] - 0),
    [date] + [time]
FROM
    dbo.[W3CLog]

The bottom is deterministic.

0

精彩评论

暂无评论...
验证码 换一张
取 消