如何将在SQL查询中存储/返回为时间戳的值转换为dd-mm-yyy

时间:2019-04-24 20:37:28

标签: postgresql datetime

我有一个查询,其中列sb.start_time AS shift_dateMIN(cs.start_time) AS first_completed_shift返回的值存储为时间戳,但我希望格式仅为dd-mm-yyyy

SELECT DISTINCT
 sb.start_time AS shift_date,  --I want this as dd-mm-yyyy format
 i.title AS industry,
 wl.name AS venue_name,
 w.first_name,
 w.last_name,
 MIN(cs.start_time) AS first_completed_shift, --I want this as dd-mm-yyyy format
 i.title AS industry,
 w.interviewed_on AS induction_date,
 d.issue_date AS edbs_issue_date,
 d.created_at AS date_dbs_added,
 a.user_id  
FROM shift_bookings sb
JOIN jobs j ON sb.job_id = j.id
JOIN listings l ON j.listing_id = l.id
JOIN work_locations wl ON l.venue_id = wl.id
JOIN workers w ON sb.worker_id = w.id
JOIN completed_shifts cs ON w.id = cs.worker_id
JOIN documents d ON w.id = d.documentable_id
JOIN audits a ON d.id = a.auditable_id
JOIN industries i ON j.industry_id = i.id
WHERE sb.shift_id IN (253106)
AND d.document_type_id = 33
AND a.auditable_type = 'Document'
GROUP BY
 sb.start_time,
 wl.name,
 w.first_name,
 w.last_name,
 i.title,
 w.interviewed_on,
 d.issue_date,
 d.created_at,
 a.user_id

1 个答案:

答案 0 :(得分:0)

如果您想要一个合适的date值,则将时间戳转换为日期:

sb.start_time::date AS shift_date

然后您需要调整group by

 MIN(cs.start_time::date) AS first_completed_shift

您还可以使用to_char()将时间戳记格式化为所需的任何格式。