在 CTE 中选择时从字符串转换日期和/或时间时转换失败

发布于 2025-01-11 13:07:40 字数 724 浏览 0 评论 0原文

我有一个问题。我不断收到错误

CASE WHEN j.PHPPDT = '00010101' THEN '0' ELSE j.PHPPDT END AS NEWPPDT,

CASE WHEN j.PHPDTE = '00010101' THEN '0' ELSE j.PHPDTE END AS NEWPROMDT

j.PHPPDTj.PHPDTE 的数据类型为 DATE

当我只运行它们时,代码似乎可以运行。然而,后来我用我的代码创建了另一个 CTE 并需要选择它们,然后我得到一个错误

 NEWPPDT AS PrintersPortDate,
 NEWPROMDT AS PromiseByDate, 

错误是:

从字符串转换日期和/或时间时转换失败。

我不太确定在哪里以及如何修复它,因为通常当发生这种情况时我只是使用

WHERE CONVERT(DATE, CONVERT(CHAR(8), NEWPPDT )) 

,但在这里它没有帮助。此外,我不确定我是否明白该将其插入哪里。我应该在我的 case 语句中或在选择列时将其插入到 j.PHPPDT 中吗...

这非常令人困惑。有人知道什么可能有帮助吗?

I have a question. I keep getting an error

CASE WHEN j.PHPPDT = '00010101' THEN '0' ELSE j.PHPPDT END AS NEWPPDT,

CASE WHEN j.PHPDTE = '00010101' THEN '0' ELSE j.PHPDTE END AS NEWPROMDT

Columns j.PHPPDT and j.PHPDTE are of datatype DATE.

When I run just them, the code seems to run. However, later with my code I create another CTE and need to select them and then I get a mistake

 NEWPPDT AS PrintersPortDate,
 NEWPROMDT AS PromiseByDate, 

The error is:

Conversion failed when converting date and/or time from character string.

I am not really sure where and how to fix it, because normally when such things happen I just use

WHERE CONVERT(DATE, CONVERT(CHAR(8), NEWPPDT )) 

but here it doesn't help. Further, I am not sure I understand where to even insert it. Should I insert it to j.PHPPDT in my case statement or when I select the column...

It is pretty confusing. Does someone know what might help?

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

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

发布评论

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

评论(1

仄言 2025-01-18 13:07:40

您可以尝试使用一些默认日期值而不是“0”

CASE WHEN j.PHPPDT = '00010101' THEN '12/31/2099' ELSE j.PHPPDT END AS NEWPPDT,
CASE WHEN j.PHPDTE = '00010101' THEN '12/31/2099' ELSE j.PHPDTE END AS NEWPROMDT

You can try using some default date value instead of '0'

CASE WHEN j.PHPPDT = '00010101' THEN '12/31/2099' ELSE j.PHPPDT END AS NEWPPDT,
CASE WHEN j.PHPDTE = '00010101' THEN '12/31/2099' ELSE j.PHPDTE END AS NEWPROMDT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文