英文:
when try to convert varchar data type (mixed date formats) to a datetime data type resulted in an out-of-range value
问题
以下是要翻译的内容:
这些是存储在我的 started_at
列中的一些数值:
31/5/2022 10:27
1/5/2022 17:03
24/6/2022 9:54
.
.
.
2022-08-09 19:09:55
2022-08-18 14:58:56
2022-11-17 22:50:52
有超过 1'000'000 行数据,并且具有这两种混合日期格式。
因此,当我执行以下 SQL 尝试将其转换为 datetime
数据类型时:
SELECT
CONVERT(VARCHAR(30), CAST(started_at AS DATETIME), 103)
FROM
[dbo].[testCyclistic12m]
我收到以下错误消息:
Msg 242,级别 16,状态 3,第 39 行
将 varchar 数据类型转换为 datetime 数据类型导致超出范围的值。
我不知道为什么会出现这个错误...
有人可以帮助我吗?我会非常感激。
英文:
These are some values stored in my started_at
column:
31/5/2022 10:27
1/5/2022 17:03
24/6/2022 9:54
.
.
.
2022-08-09 19:09:55
2022-08-18 14:58:56
2022-11-17 22:50:52
There are more than 1'000'000 rows and have mixed date formats like these 2.
So when I execute this SQL to try and convert to datetime
datatype:
SELECT
CONVERT(VARCHAR(30), CAST(started_at AS DATETIME), 103)
FROM
[dbo].[testCyclistic12m]
I get this error:
> Msg 242, Level 16, State 3, Line 39
> The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
I don't know why....
Can anyone help me? I'd appreciate it very much
答案1
得分: 1
以下是翻译好的部分:
SELECT COALESCE(try_convert(datetime, col1, 103), try_convert(datetime, col1, 121 )) AS converted_col1
, col1
FROM (
VALUES (N'31/5/2022 10:27 ')
, (N'1/5/2022 17:03')
, (N'24/6/2022 9:54 ')
, (N'2022-08-09 19:09:55 ')
, (N'2022-08-18 14:58:56')
, (N'2022-11-17 22:50:52')
) t (col1)
它首先尝试将日期转换为dd/mm/yyyy格式,然后尝试将其转换为常规的yyyy-mm-dd格式。
输出:
converted_col1 | col1 |
---|---|
2022-05-31 10:27:00.000 | 31/5/2022 10:27 |
2022-05-01 17:03:00.000 | 1/5/2022 17:03 |
2022-06-24 09:54:00.000 | 24/6/2022 9:54 |
2022-09-08 19:09:55.000 | 2022-08-09 19:09:55 |
2022-08-18 14:58:56.000 | 2022-08-18 14:58:56 |
2022-11-17 22:50:52.000 | 2022-11-17 22:50:52 |
英文:
Something like this perhaps:
SELECT COALESCE(try_convert(datetime, col1, 103), try_convert(datetime, col1, 121 )) AS converted_col1
, col1
FROM (
VALUES (N'31/5/2022 10:27 ')
, (N'1/5/2022 17:03')
, (N'24/6/2022 9:54 ')
, (N'2022-08-09 19:09:55 ')
, (N'2022-08-18 14:58:56')
, (N'2022-11-17 22:50:52')
) t (col1)
It tries to cast to dd/mm/yyyy style first, and the regular yyyy-mm-dd in the second.
Output:
converted_col1 | col1 |
---|---|
2022-05-31 10:27:00.000 | 31/5/2022 10:27 |
2022-05-01 17:03:00.000 | 1/5/2022 17:03 |
2022-06-24 09:54:00.000 | 24/6/2022 9:54 |
2022-09-08 19:09:55.000 | 2022-08-09 19:09:55 |
2022-08-18 14:58:56.000 | 2022-08-18 14:58:56 |
2022-11-17 22:50:52.000 | 2022-11-17 22:50:52 |
答案2
得分: 0
你面临一个非常困难的问题,需要以稳健的方式解决。您希望"垃圾进,好数据出"。
CAST(started_at AS DATETIME)
对于这个问题不适用,因为它不能接受所有可能的日期/时间文本格式。您需要以某种方式检测每行中存在的日期格式,并在CONVERT(DATETIME, started_at, style)
中使用适当的style值。
如果不知道您的表中有多少不同的文本格式,很难给出更具体的建议。
英文:
You have an extraordinarily difficult problem to solve in a robust way. You want "garbage in, good data out".
CAST(started_at AS DATETIME)
is unsuitable for this problem because it doesn't ingest all imaginable date / time text formats. You need somehow to detect which date format is present in each row and use the appropriate style value in CONVERT(DATETIIME, started_at, style)
.
Without knowing how many different text formats are in your table, it's difficult to give you more specific advice.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论