PG中的⼏种数据类型转换⽅式
PG中的⼏种数据类型转换⽅式
1、通过格式化函数进⾏转换
函数返回类型描述⽰例to_char(timestamp,text)text把时间戳转换成字符串to_char(current_timestamp,‘HH12:MI:SS’) to_char(interval,text)text把间隔转换成字符串to_char(interval ‘15h 2m 12s’,'HH24:MI:SS) to_char(int,text)text把整数转换成字符串to_char(125,'999) to_char(numeric,text)text把数字转换成字符串to_char(-125.8,‘999D99S’) to_date(text,text)date把字符串转换成⽇期to_date(‘05 Dec 2000’,‘DD Mon YYYY’) to_number(text,text)numeric把字符串转换成数字to_number(‘12,454.8-’,'99G999D9S) to_timestamp(text,text)timestamp把字符串转换成时间戳to_timestamp(‘05 Dec 2000’,‘DD Mon YYYY’) 2、使⽤cast函数进⾏转换string转date的方法
将varchar字符串转换成text类型:
select cast(varchar'123'as text);
将varchar字符类型转换成int4类型:
select cast(varchar'123'as int4);
3、通过::操作符进⾏转换
⽰例:
select1::int4 2/3::numeric;