dis*_*kid 3 t-sql sql-server date
我正在从键/值表中读取一个值,该值应包含格式的NULLABLE日期yyyy-MM-dd。
查询的简化版本是。
SELECT CAST(ClaimDate AS DATE) AS ClaimDate
FROM MyTable
Run Code Online (Sandbox Code Playgroud)
这导致错误消息:
从字符串转换日期和/或时间时转换失败。
我也尝试了CONVERT(DATE, ClaimDate)无济于事。
现在,除了知道为什么会出现此错误之外,我还很想知道是哪个值导致了此错误。
我已经看到有关上述错误消息的几个问题。但是我还没有找到能回答我问题的人。
当然,除了获得不良价值外,提出解决方案也受到高度赞赏。
Depending on your SQL Server version (2012+), you can use TRY_CONVERT:
SELECT ClaimDate
FROM MyTable
WHERE TRY_CONVERT(DATE, ClaimDate ) IS NULL
AND ClaimDate IS NOT NULL
Run Code Online (Sandbox Code Playgroud)
This will give you the values that can't be converted to a date, how you're going to solve those issues is another question. Also, a value of 2019-01-12 may be converted, but if it's in a string you cannot be sure whether this is a date in December or in January. You may get valid but wrong dates!
| 归档时间: |
|
| 查看次数: |
49 次 |
| 最近记录: |