Why is a cast from DATE to VARCHAR non-deterministic?

前端 未结 1 970
后悔当初
后悔当初 2020-12-20 23:02

In trying to add a computed column to a SQL Server table, I\'ve found that casting a column with a DATE type directly to a VARCHAR is considered non-deterministic. However,

相关标签:
1条回答
  • 2020-12-20 23:26

    The string (varchar) representation of a date depends on your "locale" settings (e.g. dates in UK are often represented differently than in the US).

    In your example above, your first CAST() explicitly specifies the format of the varchar, but the second one forces the database to examine its locale settings to determine how to format the varchar result.

    The simple fact that the conversion depends on something external to the CAST() function makes it non-deterministic.

    In other words, you run the CAST() with one locale setting, change the locale then run the SAME CAST() again, and you get a different result. This is the definition of non-deterministic behavior.

    0 讨论(0)
提交回复
热议问题