Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 fixed). 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.

like image 247
Randall Avatar asked Feb 01 '26 10:02

Randall


1 Answers

CASE WHEN ISDATE(closeddate) = 1 THEN CAST(c.closeddate AS DATETIME) ELSE NULL END
like image 178
Paul Creasey Avatar answered Feb 04 '26 00:02

Paul Creasey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!