postgre如何在sql中获得正确的数据类型和日期



要求

first_date独奏者与管弦乐队合作的第一个日期,格式为"2015年1月1日"(即,月为整数,短月名称,年为整数(。

last_date独奏者与管弦乐队的最后一次演出日期,格式为"2015年1月1日"(即,月为整数,短月名称,年为整数(。

这是我的代码

SELECT 
To_char(min(date)::date, 'MM Mon YYYY') as first_date,
To_char(max(date)::date, 'MM Mon YYYY') as last_date,
FROM soloists join concerts using (id) limit 4

这就是输出。视觉输出正是我想要的,但我不知道它是否满足上面的数据类型要求。请帮帮我,谢谢。

first_date       last_date
10 Oct 1980      01 Jan 2014
11 Nov 1979      12 Dec 2011
01 Jan 1980      11 Nov 2015
10 Oct 1961      06 Jun 2009

函数to_char返回文本类型和所需格式的日期类型。如果您需要在整数类型上分别使用年份和日期,则可以使用extract函数。示例:

select 
extract(day from min(date)) as first_day, 
extract(year from min(date)) as first_year, 
To_char(min(date), 'Mon') as first_month
from soloists join concerts using (id) limit 4

结果数据字段first_day和first_year为整数类型。

最新更新