当前位置:网站首页>PG SQL截取字符串到指定字符位置
PG SQL截取字符串到指定字符位置
2022-04-23 06:03:00 【若小鱼】
今天在做PG数据到HIVE的数据交换任务时,因为某个字段在PG中是Varchar类型,hive是bigint,而偏偏PG 中该字段的存储值都被加了小数点位,导致字段类型转换失败。
现在就需要将字符串中小数点后的部分给截掉。
开始时尝试使用的是CHARINDEX来获取小数点的位置,然后使用substring函数截取该位置之前的数值。
select CAST(SUBSTRING(sal_qty, 1 , CHARINDEX('.',sal_qty)-1)as bigint)
但是运行时发现PG中没有CHARINDEX函数。
SQL 错误 [42883]: ERROR: function charindex(unknown, character varying) does not exist
建议:No function matches the given name and argument types. You might need to add explicit type casts.
在官方文档中查找相应的替换函数
https://www.postgresql.org/docs/current/functions-string.html
找到一个position函数
position ( substring text IN string text ) → integer
Returns first starting index of the specified substring within string, or zero if it's not present.
position('om' in 'Thomas') → 3
测试一下
select sal_qty, cast(SUBSTRING(sal_qty, 1 , position ('.' in sal_qty) - 1) as bigint) as sal_qty_int from table_test
返回结果,符合预期。
另外还可以尝试使用strpos函数,功能和position相同,但要注意参数位置
strpos ( string text, substring text ) → integer
Returns first starting index of the specified substring within string, or zero if it's not present. (Same as position(substring in string), but note the reversed argument order.)
strpos('high', 'ig') → 2
select sal_qty, cast(SUBSTRING(sal_qty, 1 , strpos(sal_qty,'.')-1)as bigint) as sal_qty_int from table_test
版权声明
本文为[若小鱼]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44162809/article/details/121750911
边栏推荐
- Oracle redo log产生量大的查找思路与案例
- Build a cloud blog based on ECS (send blessings on the cloud Xiaobao code and draw iphone13 for free)
- Analysis of Rdam principle
- Winter combat camp hands-on combat - MySQL database rapid deployment practice lead mouse cloud Xiaobao
- 异常记录-19
- 异常记录-18
- Prometheus Thanos与Cortex组件比较
- Exception record-5
- Arranges the objects specified in the array in front of the array
- Oracle数据库性能分析之常用视图
猜你喜欢
你应该知道的 JVM 基础知识
[Lombok quick start]
try catch 不能捕获异步错误
MySQL【sql性能分析+sql调优】
冬季实战营动手实战-上云必备环境准备,动手实操快速搭建LAMP环境 领鼠标 云小宝 背包 无影
SSM项目在阿里云部署
Using Prom label proxy to implement label based multi tenant reading of Prometheus thanos
Prometheus Cortex架构概述(水平可扩展、高可用、多租户、长期存储)
Implementation of multi tenant read and write in Prometheus cortex
Winter combat camp hands-on combat - MySQL database rapid deployment practice lead mouse cloud Xiaobao
随机推荐
Ali vector library Icon tutorial (online, download)
Prometheus和Thanos Receiver的“写多租户”实现
How does VirtualBox modify the IP network segment assigned to the virtual machine in the "network address translation (NAT)" network mode
Introduction to RDMA network
virtio 与vhost_net介绍
Oracle RAC数据库实例启动异常问题分析IPC Send timeout
如何使用TiUP部署一个TiDB v5.0集群
Prometheus Cortex使用Block存储时的相关问题
基於ECS搭建雲上博客(雲小寶碼上送祝福,免費抽iphone13任務詳解)
重启Oracle监听器会中断已有连接吗
OSS云存储管理实践(体验有礼)
Problems related to Prometheus cortex using block storage
冬季实战营 动手实战-初识上云基础,动手实操ECS云服务器新手上路 领鼠标 云小宝 背包 无影
Introduction to RDMA
异常记录-8
10g数据库使用大内存主机时不能启动的问题
Prometheus Thanos快速指南
Exception record-7
ES入门学习笔记
【OSS文件上传快速入门】