【PostgreSQL】 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;

 

 

 

posted @   郭慕荣  阅读(14521)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
历史上的今天:
2021-09-22 elasticsearch的master选举机制
点击右上角即可分享
微信分享提示