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');