我正在使用date_trunc将Unix时间转换为更具可读性的格式。但是,“小时”的值返回带后缀0的年份和小时。我希望该格式在军事时间仅显示hh:mm。我该如何实现?
SELECT
actor_id AS SRT_ID,
shift_date,
DATE_TRUNC('hour', from_unixtime(shift_start)) AS login,
DATE_TRUNC('hour', from_unixtime(shift_end)) AS logout
FROM
fct_rep_schedule_details
WHERE
ds BETWEEN '2018-12-01' AND '2019-01-15'
GROUP BY 1,2,3,4
例如,我希望输出为13:00
而不是2019-01-22 13:00:00.000
。
答案 0 :(得分:0)
您不想截断日期。您要提取小时/时间。您可以使用cast()
提取时间:
SELECT actor_id AS SRT_ID,
shift_date,
DATE_TRUNC('hour', from_unixtime(shift_start)) AS login,
DATE_TRUNC('hour', from_unixtime(shift_end)) AS logout,
CAST(from_unixtime(shift_start) as time) AS login_hh,
CAST(from_unixtime(shift_end) as time) AS logout_hh,
FROM fct_rep_schedule_details
WHERE ds BETWEEN '2018-12-01' AND '2019-01-15';
如果要控制时间的格式,请使用to_char()
:
SELECT actor_id AS SRT_ID,
shift_date,
DATE_TRUNC('hour', from_unixtime(shift_start)) AS login,
DATE_TRUNC('hour', from_unixtime(shift_end)) AS logout,
TO_CHAR(from_unixtime(shift_start), 'HH:MI') AS login_hh,
TO_CHAR(from_unixtime(shift_end), 'HH:MI') AS logout_hh,
FROM fct_rep_schedule_details
WHERE ds BETWEEN '2018-12-01' AND '2019-01-15';
答案 1 :(得分:0)
您想要格式化日期,而不是缩短日期。您应该可以使用以下查询来格式化时间:
SELECT
actor_id AS SRT_ID,
shift_date,
DATE_FORMAT(from_unixtime(shift_start), '%H:%i') AS login,
DATE_FORMAT(from_unixtime(shift_end), '%H:%i') AS logout
FROM
fct_rep_schedule_details
WHERE
ds BETWEEN '2018-12-01' AND '2019-01-15'
GROUP BY 1,2,3,4
其中%H
转换为00 .. 23中的小时,%i
转换为分钟,根据prestoDB documentation