Consider the following:
SELECT [DOB] = CASE WHEN [DOB] = '-' then 'invalid date' ELSE cast([DOB] as datetime)
END
FROM myTable where myID = 73351
When DOB = '-'
, I get the error message:
Conversion failed when converting datetime from character string.
Which seems to indicate SQL Server tries to cast DOB as datetime
despite the fact it should only print 'invalid date'
.
Yet consider this which correctly returns 'invalid date'
when DOB = '-'
.
SELECT [DOB] = CASE WHEN [DOB] = '-' then 'invalid date' ELSE 'valid date'
END
FROM myTable where myID = 73351
How can I get the first statement to return 'invalid date'
when DOB = '-'
?