PG中的几种数据类型转换方式

2021/12/24 23:11:45

本文主要是介绍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函数进行转换

将varchar字符串转换成text类型:

select cast(varchar'123' as text);

将varchar字符类型转换成int4类型:

select cast(varchar'123' as int4); 

3、通过::操作符进行转换

示例:

select 1::int4 2/3::numeric;


这篇关于PG中的几种数据类型转换方式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程