1.将数值转成字符串类型:
to_char(int, text)
int为要转换的值,text为数值格式化模式
其中 “模式” 描述:9 带有指定数值位数的值;0 带前导零的值;.(句点) 小数点;,(逗号) 分组(千)分隔符
例:
SELECT to_char(12345, '') //结果''
SELECT to_char(12345, '99999')//结果‘12345’
SELECT to_char(12345, '9999999999999999999')//结果‘12345’,结果字符串前面有空格,位数跟格式化模式中9的位数有关;
SELECT to_char(12345,'999') //结果“###”,当999少于前面数字的位数,则会显#
2.将字符串转换为数值类型:
to_number(text,text)参数1是要转的数字字符串,参数2为模式参数
例:
SELECT to_number('12345', '9999999999999999999')//12345
SELECT to_number('12345', '99999')//12345
SELECT to_number(''||12345, '9999')//1234,由于模式是4位,结果忽略最后一位;
SELECT to_number(' 12345', '9999999999999999999')//12345
SELECT to_number(' ab ,1,2a3,4b5', '9999999999999999999')//12345,会忽略所 有字符串中非数字字符
3.上月天数:
select EXTRACT(DAY from date_trunc('month', CURRENT_DATE)- interval '1 day');
当前时间和日期:
select now();
select current_timestamp;
当前年月日:
select current_date;
当前年当前月:
select extract(year from now());
select extract(month from now());
4.字符串与时间的转换
1)将当前年月日转换成字符串:
select to_char( current_date,'yyyy-mm-dd');
2) 将字符串转换为时间:
select to_date('20180201','yyyymmdd');
5.内置函数generate_series,可以按不同的规则产生一系列的填充数据
用法:
在test表中插入10行0-1000的数据:
insert into test select generate_series(1,10) as key ,(random() * 1000) :: integer;
其余详细资料,参考:http://www.cnblogs.com/stephen-liu74/archive/2012/05/04/2294643.html
ps:该博客中提供的所有信息均源自PostgreSQL官方文档,编写该篇博客的主要目的是便于今后的查阅,特此声明。