Mete
Mete

Reputation: 27

sql Cast when datetime column null or empty

like to cast regdate col as datetime when regdate column not null or empty and regdate older than 2 years.if it is empty I like to insert regdate as 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 = ''

Upvotes: 0

Views: 14218

Answers (1)

mrtig
mrtig

Reputation: 2267

You should be able to do this:

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

Upvotes: 5

Related Questions