3

I was looking for a deterministic truncate function for datetime and this one did the job:

DATEADD(dd, DATEDIFF(dd, 0, @date), 0)

But this is supposed to be the input to a persisted computed column which will be a part of the primary key, so it has to be non-nullable. So I made this:

ISNULL(DATEADD(dd, DATEDIFF(dd, 0, @date), 0), '01.01.1900')

But now the expression became non-deterministic. Can anyone tell me why, and how I can make it deterministic?

Thanks!

ercan
  • 1,639
  • 1
  • 20
  • 34

3 Answers3

6

01.01.1900 isn't deterministic...

This is:

ISNULL(DATEADD(dd, DATEDIFF(dd, 0, @date), 0), CONVERT(datetime, '19001010', 112)))

or this

ISNULL(DATEADD(dd, DATEDIFF(dd, 0, fooDT), 0), 0)

For more, see Why is my CASE expression non-deterministic? which leads to best way to convert and validate a date string

Community
  • 1
  • 1
gbn
  • 422,506
  • 82
  • 585
  • 676
4
ISNULL(DATEADD(dd, DATEDIFF(dd, 0, @date), 0), 0)

That is, my guess is the non-deterministic part is '01.01.1900', because it depends on the locale settings.

gbn
  • 422,506
  • 82
  • 585
  • 676
Andriy M
  • 76,112
  • 17
  • 94
  • 154
0

Don't use string constant, use some arbitrary number instead of '01.01.1900'

Oleg Dok
  • 21,109
  • 4
  • 45
  • 54