[英]How to convert VARCHAR (AM/PM) to TIMESTAMP (24 h) in SQL (Teradata v17)
I've tried multiple solutions, but I keep getting errors.我尝试了多种解决方案,但我不断收到错误。 I need to create a new column casting VARCHAR to TIMESTAMP that includes AM, PM or -ideally- changes it to 24 hrs format .
我需要创建一个将 VARCHAR 转换为TIMESTAMP的新列,其中包括 AM、PM 或 - 理想情况下 - 将其更改为24 小时格式。
VARCHAR format (Start_Date column): 8/3/2022 4:58:49 PM VARCHAR 格式(Start_Date 列):8/3/2022 4:58:49 PM
I found the below solution is some other post, but I'm getting error: ' Format code appears twice '我发现以下解决方案是其他帖子,但出现错误:'格式代码出现两次'
SELECT itab.*,
TO_TIMESTAMP(Start_Date, 'MM/DD/YYYY HH:MM:SS AM') AS start_TS
FROM db.info_table itab
Please advise.请指教。
You have two problems.你有两个问题。
select to_timestamp(regexp_replace('8/3/2022 4:58:49 PM', '\b([0-9])\b', '0\1'), 'MM/DD/YYYY HH:mi:SS AM')
TO_TIMESTAMP returns a TIMESTAMP(6). TO_TIMESTAMP 返回一个 TIMESTAMP(6)。 If you don't want microseconds you can specify the precision using
如果您不想要微秒,您可以使用指定精度
CAST(RegExp_Replace(start_date, '\b([0-9])\b', '0\1') AS timestamp(0) FORMAT 'MM/DD/YYYYbHH:Mi:SSbT')
All you need is pad day and month in Teradata (as opposed to Oracle etc).您只需要在 Teradata 中填充日期和月份(而不是 Oracle 等)。 m/d/y format has not been implemented.
m/d/y 格式尚未实施。
select '8/3/2022 4:58:49 PM' date1,
to_timestamp(
lpad(strtok(date1,'/',1),2,'0')||'/'||lpad(strtok(date1,'/',2),2,'0')||'/'||strtok(date1,'/',3)
,'mm/dd/yyyy hh24:mi:ss AM'
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.