sql在datetime列为null或为空时进行转换

时间:2013-08-22 02:55:00

标签: sql datetime

喜欢将regdate col作为日期时间,当regdate列不为null或为空并且regdate超过2年时。如果它为空我想插入regdate为1900-01-01。

id  name    regdate
555 bob     24/06/2013 00:00:00
556 tom     24/07/2013 00:00:00
557 jack    24/10/2008 00:00:00
558 susan   24/11/2009 00:00:00
559 emily   
560 Mert    


SELECT *, CAST(idate AS DATETIME) AS finalDate
    INTO #tempFinal
    FROM #tempA
    WHERE CAST(idate AS DATETIME) < DATEADD(YEAR, -2, GETDATE())
    OR idate IS NULL OR idate = ''

1 个答案:

答案 0 :(得分:5)

你应该可以这样做:

CAST(ISNULL(Idate, '1900-01-01') AS DATE)