使用sql将字符串转为时间戳

问题描述 投票:0回答:1

有一列名称为request_time,其数据类型为字符串。数据类似于 01-03-2024 16:58:44。我想将其转换为配置单元中的时间戳。我已经尝试过一些事情,例如:

SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(request_time, 'dd-MM-yyyy HH:mm:ss')) AS converted_timestamp FROM ...;
 
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(REGEXP_REPLACE(request_time, '(\\d{2})-(\\d{2})-(\\d{4}) (\\d{2}):(\\d{2}):(\\d{2})', '$3-$2-$1 $4:$5:$6'))) AS converted_timestamp FROM ...;
 
SELECT from_unixtime(unix_timestamp(request_time, 'dd-MM-yyyy HH:mm:ss'), 'dd-MM-yyyy HH:mm:ss') AS converted_timestamp FROM ...;
 
SELECT CAST(request_time AS TIMESTAMP) AS date_request from ...;

但以上都不起作用。在某些情况下,数据仍然保留为字符串,在其他情况下它变成空。

sql database hive timestamp impala
1个回答
0
投票

你可以试试这个方法吗

CAST(FROM_UNIXTIME(UNIX_TIMESTAMP(request_time,'dd-MM-yyyy HH:mm:ss')) as TIMESTAMP) as converted_date
© www.soinside.com 2019 - 2024. All rights reserved.