I'm querying the back-end of an application. For what ever reason the developer decided to use NVARCHAR for all fields. I'm having a problem with one of the "date" fields--somehow it got populated with "97630886". There's a companion "time" field and for my query I'm trying to concatenate and CAST the results as a DATETIME. I'm trying to use a CASE statement but it's erroring: "The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.". Apparently all conditions are evaluated? Is there a way I can rewrite my SELECT statement to handle this?
SELECT CASE
WHEN LOG_DATE IS NULL AND LOG_TIME IS NULL THEN NULL
WHEN LOG_DATE IS NULL THEN LOG_TIME
WHEN LOG_TIME IS NULL THEN LOG_DATE
WHEN ISDATE(LOG_DATE) = 0 OR ISDATE(LOG_TIME) = 0 THEN LOG_DATE + ' ' + LOG_TIME
ELSE CAST(LOG_DATE + ' ' + LOG_TIME AS DATETIME)
END AS [LogDateTime]
FROM ...