接收错误时间戳' 5/9/2022 11:09'未被认可

发布于 2025-01-29 11:34:34 字数 100 浏览 4 评论 0原文

尝试在雪花中转换此日期格式时会出现此错误。

选择to_timestamp('5/9/2022 11:09')

时间戳记'5/9/2022 11:09'尚未确认

Gives this error when trying to convert this date format in snowflake.

SELECT TO_TIMESTAMP('5/9/2022 11:09')

Timestamp '5/9/2022 11:09' is not recognized

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

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

发布评论

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

评论(5

装迷糊 2025-02-05 11:34:34

您需要提供格式字符串:

SELECT TO_TIMESTAMP('5/9/2022 11:09', 'DD/MM/YYYY HH:MI');

-- Or if US based date format:
SELECT TO_TIMESTAMP('5/9/2022 11:09', 'MM/DD/YYYY HH:MI');

这是必需的,因为5/9/2022是模棱两可的。在欧洲是9月5日。在美国是5月9日。

You need to provide a format string:

SELECT TO_TIMESTAMP('5/9/2022 11:09', 'DD/MM/YYYY HH:MI');

-- Or if US based date format:
SELECT TO_TIMESTAMP('5/9/2022 11:09', 'MM/DD/YYYY HH:MI');

This is required because 5/9/2022 is ambiguous. In Europe it's September 5. In the US it's May 9.

素染倾城色 2025-02-05 11:34:34

您正在使用任意的时间戳格式,希望DBM会猜测它的含义。我必须承认我不能。这是5月9日还是9月5日?

您可能很幸运,DBM接受这种格式,甚至正确地猜测。但是,相反,如果依靠运气,则应向DBM提供所需的信息,即格式。或者最好仍然立即使用时间戳文字:

SELECT TIMESTAMP '2022-05-09 11:09:00'

You are using an arbitrary timestamp format hoping the DBMS will guess what it means. I must admit I can't. Is this May 9 or September 5?

You may be lucky that the DBMS accepts this format and even guesses right. But instead if relying on luck, you should give the DBMS the information it needs, i.e. the format. Or better still use a timestamp literal right away:

SELECT TIMESTAMP '2022-05-09 11:09:00'
半枫 2025-02-05 11:34:34
SELECT column1, TO_TIMESTAMP(column1, 'mm/dd/yyyy hh:mi') as ts
FROM VALUES
  ('5/9/2022 11:09'),
  ('5/12/2022 12:09'),
  ('5/24/2022 23:09');
第1列TS
5/9/2022 11:092022-05-09 11:09:00.000
5/12/2022 12:0912:09:00.000
5/24/20222022-05-12 23:09 2022-05-05-05-05--05--05--05--05--05--05--05--05--05- 24 23:09:00.000

因此,您“这是针对大数据集”没有意义。.

您是指数据集具有混合格式的数据吗?

在这一点上,可以使用try_函数,以按等级顺序进行尝试:

SELECT 
    column1 
    ,TRY_TO_TIMESTAMP(column1, 'mm/dd/yyyy hh:mi') as ts_a
    ,TRY_TO_TIMESTAMP(column1, 'mm/dd/yyyy') as ts_b
    ,TRY_TO_TIMESTAMP(column1, 'mm/dd/yyyy hh:mi:ss.ff6') as ts_c
    ,coalesce(ts_a, ts_b, ts_c) as ts
FROM VALUES
('5/9/2022 11:09'),
('5/12/2022'),
('5/24/2022 23:09'),
('5/24/2022 23:09:59.99999');
第1列TS_ATS_BTS_CTS
5/9/2022 11:092022-05-09 11:09:00.000null Null NullNull2022-05-09 11:09 11:09 11:09 :00.000
5/12/2022NULL2022-05-12 00:00:00.0002022-05-1200:00:00:00.000
23:092022-05-24 23:00.005/24/2022NULL -05-24 23:09:00.000
5/24/2022 23:09:59.99999无效NullNull Null2022-05-24 23:09:59.9992022-05-24 23:09:09:59.999

,这可以合并为一个命令要清理:

SELECT 
    column1 
    ,coalesce(TRY_TO_TIMESTAMP(column1, 'mm/dd/yyyy hh:mi'), TRY_TO_TIMESTAMP(column1, 'mm/dd/yyyy'), TRY_TO_TIMESTAMP(column1, 'mm/dd/yyyy hh:mi:ss.ff6')) as ts
FROM VALUES
('5/9/2022 11:09'),
('5/12/2022'),
('5/24/2022 23:09'),
('5/24/2022 23:09:59.99999');
第1栏TS
5/9/2022 11:092022-05-09 11:09:00.000
5/12/20222022-2022-05-12 00:00:00.000
5/24/20222222222222222222222222223: 09 2022-23:09 2022- 05-24 23:09:00.000
5/24/2022 23:09:59.99992022-05-2423:09:59.999
SELECT column1, TO_TIMESTAMP(column1, 'mm/dd/yyyy hh:mi') as ts
FROM VALUES
  ('5/9/2022 11:09'),
  ('5/12/2022 12:09'),
  ('5/24/2022 23:09');
COLUMN1TS
5/9/2022 11:092022-05-09 11:09:00.000
5/12/2022 12:092022-05-12 12:09:00.000
5/24/2022 23:092022-05-24 23:09:00.000

so you "this is for a large data set" does not make sense..

do you mean you data set has mixed formatted data?

at which point the TRY_ functions can be used, to try things in rank order:

SELECT 
    column1 
    ,TRY_TO_TIMESTAMP(column1, 'mm/dd/yyyy hh:mi') as ts_a
    ,TRY_TO_TIMESTAMP(column1, 'mm/dd/yyyy') as ts_b
    ,TRY_TO_TIMESTAMP(column1, 'mm/dd/yyyy hh:mi:ss.ff6') as ts_c
    ,coalesce(ts_a, ts_b, ts_c) as ts
FROM VALUES
('5/9/2022 11:09'),
('5/12/2022'),
('5/24/2022 23:09'),
('5/24/2022 23:09:59.99999');
COLUMN1TS_ATS_BTS_CTS
5/9/2022 11:092022-05-09 11:09:00.000nullnull2022-05-09 11:09:00.000
5/12/2022null2022-05-12 00:00:00.000null2022-05-12 00:00:00.000
5/24/2022 23:092022-05-24 23:09:00.000nullnull2022-05-24 23:09:00.000
5/24/2022 23:09:59.99999nullnull2022-05-24 23:09:59.9992022-05-24 23:09:59.999

and that can be merge into a single command to clean that up:

SELECT 
    column1 
    ,coalesce(TRY_TO_TIMESTAMP(column1, 'mm/dd/yyyy hh:mi'), TRY_TO_TIMESTAMP(column1, 'mm/dd/yyyy'), TRY_TO_TIMESTAMP(column1, 'mm/dd/yyyy hh:mi:ss.ff6')) as ts
FROM VALUES
('5/9/2022 11:09'),
('5/12/2022'),
('5/24/2022 23:09'),
('5/24/2022 23:09:59.99999');
COLUMN1TS
5/9/2022 11:092022-05-09 11:09:00.000
5/12/20222022-05-12 00:00:00.000
5/24/2022 23:092022-05-24 23:09:00.000
5/24/2022 23:09:59.999992022-05-24 23:09:59.999
权谋诡计 2025-02-05 11:34:34

请执行查询,包括如下

select to_timestamp('5/9/2022 11:09:00');

Please execute the query including seconds like below

SELECT TO_TIMESTAMP('5/9/2022 11:09:00');

ゃ懵逼小萝莉 2025-02-05 11:34:34

以前的MySQL版本允许使用“放松”的日期表达式。这意味着您可以在日期零件中使用任何分隔符,例如2004@04@04@162022 = 09 = 17,<代码> 1968.04.22 ,都被认为是有效的。

但是从MySQL 8.0.29开始,不再允许这一点。现在,您必须将-(连字符)用作定界符。

您可以阅读有关更多信息

Previous MySQL versions allowed for the use of a "relaxed" date expression. This means that you could use any delimiters at all for the date parts, e.g. 2004@04@16, 2022=09=17, 1968.04.22, were all considered to be valid.

But as of MySQL 8.0.29, this is no longer allowed. You now have to use a - (hyphen) as the delimiter.

You can read the reference manual for more information

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文