更改PostgreSQL查询结果的日期格式

时间:2014-02-26 18:30:49

标签: sql postgresql date

我有以下查询

select substring(listDate from '............$') as v_end_date,
substring(listDate from '^...............') as v_start_date

现在listDate值可以像

select substring('06 Jan 2014 to 12 Jan 2014,
 13 Jan 2014 to 19 Jan 2014,
 20 Jan 2014 to 26 Jan 2014
' from '............$') as v_end_date,
substring('06 Jan 2014 to 12 Jan 2014,
 13 Jan 2014 to 19 Jan 2014,
 20 Jan 2014 to 26 Jan 2014
' from '^............') as v_start_date

以上查询结果

V_END_DATE  V_START_DATE
26 Jan 2014 06 Jan 2014

现在我需要v_end_datev_start_date格式,如yyyy-mm-ddMon 06 Jan 2014

1 个答案:

答案 0 :(得分:4)

使用date将您的字符串转换为实际的to_date(),然后使用to_char()获得您喜欢的任何格式。

演示:

SELECT to_char(day, 'YYYY-MM-DD')     AS format1
     , to_char(day, 'Dy DD Mon YYYY') AS format2 
FROM   (SELECT to_date('26 Jan 2014', 'DD Mon YYYY') AS day) sub