Presto SQL – 转换字符型日期为日期格式时出现问题

huangapple go评论78阅读模式
英文:

Presto SQL - Trouble with converting date in varchar to date format

问题

在Presto-SQL中,我在将varchar格式的日期列转换为日期格式时遇到了问题。日期列的格式如下:'9/5/2022','12/23/2022','%null%'等。

我尝试了以下查询:

SELECT
DATE_PARSE(date_column, '%m/%d/%Y')
FROM table

SELECT
CAST(date_column AS DATE)
FROM table

但总是出现以下错误:

Caused by: io.prestosql.jdbc.$internal.client.FailureInfo$FailureException: Invalid format: "11/30/2022" is malformed at "/30/2022"

英文:

I am having an issue converting a date column in varchar format to date format in presto-sql. The date_column is in this format : '9/5/2022', '12/23/2022', '%null%' etc.

I have tried the following queries:

SELECT
DATE_PARSE(date_column, '%Y-%m-%d')
FROM table

SELECT
CAST(date_column AS DATE)
FROM table

And there is always this error popping up:
> Caused by: io.prestosql.jdbc.$internal.client.FailureInfo$FailureException: Invalid format: "11/30/2022" is malformed at "/30/2022"

答案1

得分: 1

The provided format (see the docs) represents year, month, day separated by - while your data is in month, day, year separated by /. Change the format accordingly:

Output:

SELECT DATE_PARSE('11/30/2022', '%m/%d/%Y');

Output:

          _col0
-------------------------
 2022-11-30 00:00:00.000

Note that strings like '%null%' (not just null strings) will be invalid also. If you really have those - you should consider wrapping the parse call into try.

英文:

The provided format (see the docs) represents year, month, day separated by - while your data is in month, day, year separated by /. Change the format accordingly:

SELECT DATE_PARSE('11/30/2022', '%m/%d/%Y');

Output:

          _col0
-------------------------
 2022-11-30 00:00:00.000

Note that strings like '%null%' (not just null strings) will be invalid also. If you really have those - you should consider wrapping the parse call into try.

huangapple
  • 本文由 发表于 2023年2月6日 09:26:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定