在with
陈述中:
WITH queryResult AS
(
SELECT
CONVERT(DATETIME, '2017-12-12 ' + PARTIALDATE)
FROM
tableA
WHERE
ISDATE('2017-12-12 ' + PARTIALDATE) = 1
)
....
在这个区块中,我将获得例外:
从字符串转换日期和/或时间时转换失败。
所以我做了一些改变:
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
)
它有效!
但为什么?
答案 0 :(得分:1)
WHERE
在转换发生之前不会进行过滤。案例陈述保证了操作顺序。以下应该有效:
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
)