【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;
郭慕荣博客园
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
2021-09-22 elasticsearch的master选举机制