1
我想用另一個日期替換一個特定的日期時間值。我試了一下:SQL Server - 用另一個日期替換datatime字段
SELECT REPLACE(t.date,'1999-01-01 00:00:00.000','1900-01-01 00:00:00.000')
FROM table t
但是,這導致到
Jan 1 1999 12:00AM
我在做什麼錯在這裏?
我想用另一個日期替換一個特定的日期時間值。我試了一下:SQL Server - 用另一個日期替換datatime字段
SELECT REPLACE(t.date,'1999-01-01 00:00:00.000','1900-01-01 00:00:00.000')
FROM table t
但是,這導致到
Jan 1 1999 12:00AM
我在做什麼錯在這裏?
你不能在一個datetime
數據類型使用replace()
,但你可以使用的isnull()
和nullif()
,或case
表達的組合。
使用isnull(nullif())
:使用case
表達
select isnull(nullif(t.date,'1999-01-01 00:00:00.000'),'1900-01-01 00:00:00.000')
from table t
:
select case when t.date = '1999-01-01 00:00:00.000'
then '1900-01-01 00:00:00.000'
else t.date
end
from table t
rextester演示:http://rextester.com/QMI12865
這是特定於一個日期,如圖所示,或者是邏輯更復雜?你只是想讓1999年成爲1900年,還是你希望每個日期能夠回落99年? – 3BK
@ 3BK這只是特定的日期。 – DenStudent