views:

69

answers:

2

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.

+4  A: 

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?

gbn
Yeah, no need to subtract, indeed. That did the trick.
John Gietzen
A: 

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.

John Gietzen