How to ignore bad characters in a SQL query
The short story is I have a SQL Server DB with varchar fields instead of datetime (don't ask, it's a long story and can't be f开发者_JAVA技巧ixed). Somehow we've recently been getting weird / random characters inserted in these fields instead of what should be there (either NULL, '' or YYYY-MM-DD). Something like this: '?+x' with high-bit ascii characters.
The report uses this query to help massage the data into something usable (only relevant parts posted here):
SELECT CASE WHEN c.CallStatus = 'Closed' THEN CAST(c.ClosedDate + ' ' + c.ClosedTime as datetime) ELSE NULL END as 'Closed Date'
WHERE CAST(c.closeddate AS DATETIME) BETWEEN @StartDate AND @EndDate
but it is choking on this new bad data.
My question is this:
How can I update the query to ignore the bad data so I can get the reports to run while I hunt down the source of the bad data? My first priority is to get the reports to function, second is to find and kill the source of bad data.
CASE WHEN ISDATE(closeddate) = 1 THEN CAST(c.closeddate AS DATETIME) ELSE NULL END
Looks to be all fixed, thankyouverymuch! here's my final result:
AND case WHEN ISDATE (closeddate) = 1 THEN CAST(c.closeddate AS DATETIME) ELSE NULL END BETWEEN @StartDate AND @EndDate
and now on to the hunt!
精彩评论