postgresql 部分函数整理

1.extract
    select extract(hour from '2016-05-06 23:00:01'::timestamp); --返回23
返回指定部分的值。

2.date_trunc
    select date_trunc('hour','2016-05-06 23:25:01'::timestamp); --返回 2016-05-06 23:00:00
返回时间串,但是将指定部分之后的内容格式话

3. coalesce(字段名,默认值)
    若字段为空 填充默认值。

3 age(timestamp ,timestamp )  或者 age(timestamp )
    age(timestamp '19650925')
    计算从19650925 到现在的时间 得到: "50 years 8 mons 20 days"

select to_char(age(timestamp '19650925'),'YYY') 得到年: "050"

cast('123' as integer); 转化为数值
     select cast(to_char(age(timestamp '19650925'),'YYY') as integer);     得到50;
    cast(timestamp as time ) 得到时分秒格式的时间  
    cast(timestamp as date )  得到年月日格式的时间

date_part('year',max(operate_time)-min(operate_time)) , 计算两个时间差 返回年

6 substring(string [from int] [for int] )

    select substring('123465789',2,3) ;  得到:234
    select substring('123465789', from 4) ;  得到:465789    
    select substring('123465789', from 4  for 2) ;  得到:46  

7    字符 串列转行函数  string_agg

8    字符 串行转列  regexp_split_to_table


9   generate_series(start,stop,[step]);   按不同的规则用来产生一系列的填充数据。

     postgres=# select generate_series(1,10,3);

     generate_series 
    -----------------
               1
               4
               7
              10
    (4 rows)

猜你喜欢

转载自blog.csdn.net/tangpengh/article/details/51832857