标签:string sal 截取 qty substring PG SQL position
今天在做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
标签:string,sal,截取,qty,substring,PG,SQL,position 来源: https://blog.csdn.net/weixin_44162809/article/details/121750911
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。