PostgreSQL用于转换时间戳的替代方法

时间:2014-12-27 22:53:25

标签: sql postgresql

我是PostgreSQL的新手,我想知道是否有更快/更简单的方法将时间戳转换为YY或YYYYMM格式?

**substring**

select count(1), substring(text(id_timestamp),0,8)  
from dual
where  id_timestamp >= (now()- '3 month'::INTERVAL)
group by substring(text(id_timestamp),0,8) 


**to_char**

select count(1),to_char(id_timestamp,'YYYY-MM') 
from dual
where  id_timestamp >= (now()- '3 month'::INTERVAL)
group by to_char(id_timestamp,'YYYY-MM') 

Year:

select count(1),to_char(id_timestamp,'YYYY') 
from dual
where  id_timestamp >= (now()- '3 month'::INTERVAL)
group by to_char(id_timestamp,'YYYY') 

1 个答案:

答案 0 :(得分:1)

to_char就是这样做的。为了不必重复选择列表中的表达式,可以通过它们在列表中的位置来引用它们:

select count(*), to_char(id_timestamp,'YYYY-MM') 
from t
group by 2
order by 2

Postgresql中没有dual