欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PostgreSQL截取字符串到指定字符位置詳細示例

 更新時間:2023年07月21日 11:50:32   作者:若小魚  
這篇文章主要給大家介紹了關(guān)于PostgreSQL截取字符串到指定字符位置的相關(guān)資料,PostgreSQL數(shù)據(jù)庫拼接字符串函數(shù)是一種非常重要的函數(shù),使用它可以方便地將不同的字符串進行拼接操作,從而得到我們需要的結(jié)果,需要的朋友可以參考下

今天在做PG數(shù)據(jù)到HIVE的數(shù)據(jù)交換任務(wù)時,因為某個字段在PG中是Varchar類型,hive是bigint,而偏偏PG 中該字段的存儲值都被加了小數(shù)點位,導(dǎo)致字段類型轉(zhuǎn)換失敗。

現(xiàn)在就需要將字符串中小數(shù)點后的部分給截掉。

開始時嘗試使用的是CHARINDEX來獲取小數(shù)點的位置,然后使用substring函數(shù)截取該位置之前的數(shù)值。

select CAST(SUBSTRING(sal_qty, 1 , CHARINDEX('.',sal_qty)-1)as bigint)

但是運行時發(fā)現(xiàn)PG中沒有CHARINDEX函數(shù)。

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.

在官方文檔中查找相應(yīng)的替換函數(shù)

https://www.postgresql.org/docs/current/functions-string.html

找到一個position函數(shù)

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

返回結(jié)果,符合預(yù)期。

另外還可以嘗試使用strpos函數(shù),功能和position相同,但要注意參數(shù)位置

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

 總結(jié)

到此這篇關(guān)于PostgreSQL截取字符串到指定字符位置的文章就介紹到這了,更多相關(guān)PGSQL截取字符串到指定位置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論