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

倾然丶 夕夏残阳落幕 提交于 2019-12-10 17:18:32

问题


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 pain 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.


回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/3037330/why-is-date-time-offset-non-deterministic-in-sql-server-2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!