【postgresql】常用SQL

postgresql 查询某张表的所有列名,字段信息 


SELECT
    *
FROM
    information_schema. COLUMNS
WHERE
    table_schema = '模式名'
AND TABLE_NAME = '表名';

postgresql根据出生日期计算年龄

select date_part('years', age(now(),dt_csrq));

PostgreSQL数据库切割和组合字段函数

字段组合:
1、concat(id,name,remark)
2、concat_ws('_',id,name,remark)
字段切割:
1、split_part('now|year|month','|',3);
2、regexp_split_to_table
            regexp_split_to_table('kenyon,love,,china,!',',');
3、regexp_split_to_array
            regexp_split_to_array('kenyon,love,,china,!',',');

postgres numeric类型取小数

取整:trunc(price_unit)
取小数:mod(price_unit,trunc(price_unit) )

PostgreSQL 日期格式化

to_date('2012-05-01 23:59:59','yyyy-mm-dd hh24:mi:ss');
PostgreSQL 的to_date 只会返回年月日

decimal/numeric类型的四舍五入问题‏


执行如下查询:
      select col1/col2 from test1;   --输出结果: 0.52629521235861037609
   
      select cast(col1/col2 as numeric(2,2)) from test1  --输出结果:0.52    
 
      我们看到,使用cast函数输出结果是0.52 被截断了,我们希望小数点后第3为能四舍五入,希望结果为0.53 .
 
使用round函数可以实现四舍五入功能:

       select cast(round(col1/col2,2) as numeric(3,2)) from test1  --输出:0.53 ,希望的结果
       或者使用convert函数
       select convert(numeric(3,2),round(col1/col2,2)) from test1  --输出:0.53 ,希望的结果

PostgreSQL的时间/日期函数使用

select now() + interval '2 years';
select now() + interval '1 month'; 
select now() - interval '3 week';
select now() + '10 min';  
计算两个时间差

使用 age(timestamp, timestamp)

select age(now(), timestamp '1989-02-05');
时间字段的截取
 取年份
select extract(year from now());
取月份
select extract(month from now());
select extract(day from timestamp '2013-04-13');

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值