我有一张包含这些信息的表:
July 16th 2018, 11:16:35.408 July 16th 2018, 11:16:35.408 appointmentCard flip event 5afa7e01-6045-4ff5-b2d7-1933cd16ebca
July 16th 2018, 11:16:18.649 July 16th 2018, 11:16:18.649 5afa7e01-6045-4ff5-b2d7-1933cd16ebca
我需要在两个不同的列中提取日期和小时。 我尝试过:
select
to_char(mlk.time, 'yyyy-mm-dd') as Date,
to_char(mlk.time, 'HH24:MI') as Hours
from main_source_multichannel mlk
inner join main_source_orders ord on mlk.trackingid = ord.trackingid
没有成功,Postgresql给我这个错误:
SQL Error [42883]: ERROR: function to_char(character varying) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 10
ERROR: function to_char(character varying) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 10
ERROR: function to_char(character varying) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 10
我做错了什么? 谢谢
答案 0 :(得分:2)
日期的格式使其比所需的要复杂一些。我将使用子查询将其解析为时间戳,然后提取日期和时间就变得很简单。这是如何执行此操作的示例:SQL Fiddle
在您的情况下,类似:
WITH dates AS (
SELECT to_timestamp(mlk.time, 'MonBDDBBBYYYYBBHH:MI:SS:MS') AS dateTime
FROM main_source_multichannel mlk
INNER JOIN main_source_orders ord on mlk.trackingid = ord.trackingid
)
SELECT dateTime::date AS theDate, dateTime::time AS theTime
FROM dates
正如其他答案所建议的那样,将数据存储为适当的时间戳将消除子查询的需要,而您只需要一个简单的SELECT。
答案 1 :(得分:0)
我可能会在您的SELECT
中尝试以下操作。
SELECT
to_char((mlk.time)::TIMESTAMP) , 'yyyy-mm-dd') AS date
,to_char((mlk.time)::TIMESTAMP), 'HH24:MI') AS hours
如果可以的话,我也会尝试将数据存储为TIMESTAMP
如果无法将列强制转换为::TIMESTAMP
,则可能需要修复这些记录。