Why is my CASE expression non-deterministic?

前端 未结 2 1591
夕颜
夕颜 2020-12-10 17:18

I am trying to create a persisted computed column using CASE expression:

ALTER TABLE dbo.Calendar ADD PreviousDate AS 
case WHEN [Date]>\'20100101\' THEN          


        
2条回答
  •  自闭症患者
    2020-12-10 17:52

    You need to CONVERT '20100101' with a style.

    Source or target type is datetime or smalldatetime, the other source or target type is a character string, and a nondeterministic style is specified.

    So, try this:

    ...WHEN [Date] > CONVERT(datetime, '20100101', 112)....
    

    Date parsing from string can be unreliable as I've answered before (mostly in comments)

    Edit:

    I wouldn't say it's a bug, but SQL Server asking for 100% clarification. yyyymmdd is not ISO and SQL Server parsing yyyy-mm-dd is unreliable (see my answer link)

提交回复
热议问题