与SQL SERVER中的COALESCE一起使用时GETDATE()的奇怪行为

时间:2017-05-14 23:45:46

标签: sql-server coalesce getdate

DECLARE @i INT

SET @i=14

SELECT COALESCE(@i, GetDate());

以上返回

1900-01-15 00:00:00.000

现在,如果我将i更改为11,结果将是

1900-01-12 00:00:00.000

GETDATE()不应该返回当前日期时间吗?

1 个答案:

答案 0 :(得分:2)

data type precedence

When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence.

1900-01-01 + 14是1900-01-15,你的int被转换为datetime,因为coalesce的第二个参数是datetime