如何忽略 SQL 查询中的错误字符

发布于 2024-08-17 09:13:41 字数 546 浏览 2 评论 0原文

简而言之,我有一个带有 varchar 字段而不是 datetime 字段的 SQL Server 数据库(不要问,这是一个很长的故事,无法修复)。不知何故,我们最近在这些字段中插入了奇怪/随机的字符,而不是应该存在的字符(NULL、'' 或 YYYY-MM-DD)。像这样:'?+x' 带有高位 ASCII 字符。

该报告使用此查询来帮助将数据整理为可用的内容(此处仅发布相关部分):

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

但它对这个新的不良数据感到窒息。

我的问题是:

如何更新查询以忽略不良数据,以便在追查不良数据来源时运行报告?我的首要任务是让报告发挥作用,其次是找到并消除不良数据的来源。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

女皇必胜 2024-08-24 09:13:41
CASE WHEN ISDATE(closeddate) = 1 THEN CAST(c.closeddate AS DATETIME) ELSE NULL END
CASE WHEN ISDATE(closeddate) = 1 THEN CAST(c.closeddate AS DATETIME) ELSE NULL END
流云如水 2024-08-24 09:13:41

看起来一切都已修复,非常感谢!这是我的最终结果:

当 ISDATE(关闭日期)= 1 时的情况
然后 CAST(c.closedate AS DATETIME)
@StartDate 和之间的 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文