gpt4 book ai didi

regex - postgres 将字符转换为日期时间

转载 作者:行者123 更新时间:2023-11-29 13:55:52 24 4
gpt4 key购买 nike

好的,我正在尝试根据文本文件名转换字符。使用正则表达式提取的名称。在 8 月 21 日这个日期之前,它可以完美运行。

select to_char(
to_date(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
substr('UserAndMasterPlanPerAugust21st2015.txt',21),
'.txt',''),
'rd','-'),
'th','-'),
'nd','-'),
'st','-'),
'MonthDD-YYYY'),
'YYYYMMDD')::integer

该代码将产生此错误。

ERROR:  invalid value "Augu-21st" for "Month"
DETAIL: The given value did not match any of the allowed values for this field.

********** 错误 **********

错误:“月份”的无效值“8 月 21 日”SQL 状态:22007详细信息:给定值与该字段的任何允许值都不匹配。

我希望这个日期的结果是 20150821

我已经知道问题出在“st”上,因为有两个“st”,我只是想用最好的方法来解决这个问题。

谢谢

最佳答案

我认为您可以使用另一个正则表达式,我建议您尝试使用这个来删除日期中的序数,例如:

(?<=[0-9])(?:st|nd|rd|th)

example here

你只需要将它转义为 postgres 方言...

关于regex - postgres 将字符转换为日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32122069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com