gpt4 book ai didi

sql-server - 在 SQL Server 中将字符串转换为日期时间时出现异常

转载 作者:行者123 更新时间:2023-12-02 02:58:00 30 4
gpt4 key购买 nike

with 语句中:

WITH queryResult AS
(
SELECT
CONVERT(DATETIME, '2017-12-12 ' + PARTIALDATE)
FROM
tableA
WHERE
ISDATE('2017-12-12 ' + PARTIALDATE) = 1
)
....

在这个 block 中,我会得到异常:

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

所以我做了一些改变:

WITH subQueryResult AS
(
SELECT
PARTIALDATE
FROM
tableA
WHERE
ISDATE('2017-12-12 ' + PARTIALDATE) = 1
),
queryResult AS
(
SELECT
CONVERT(DATETIME, '2017-12-12 ' + PARTIALDATE)
FROM
subQueryResult
)

而且有效!

但为什么呢?

最佳答案

WHERE 不会在转换发生之前进行过滤。 case 语句保证操作顺序。以下应该有效:

WITH queryResult AS
(
SELECT
CASE WHEN ISDATE('2017-12-12 ' + PARTIALDATE) = 1 THEN CONVERT(DATETIME, '2017-12-12 ' + PARTIALDATE) ELSE NULL END
FROM
tableA
WHERE
ISDATE('2017-12-12 ' + PARTIALDATE) = 1
)

关于sql-server - 在 SQL Server 中将字符串转换为日期时间时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48162195/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com