[英]how to replace the first “9” in a string with another character in postgresql database column?
我在“測試”表中有一列“ pnum”。 我想為每條記錄將pnum中的前導“ 9”替換為“ *”。
testdb=# select * from test limit 5;
id name pnum
===========================================
1 jk 912312345
2 tt 9912333333
我希望pnum看起來像這樣:
id name pnum
===========================================
1 jk *12312345
2 tt *912333333
我將如何在postgres中做類似的事情?
編輯1:
到目前為止,我已經嘗試過類似的方法:
select id, name, '*' && substring(pnum FROM 2 FOR CHAR_LENGTH(pnum)-1 ) from test limit 3;
還嘗試了以下方法:
select id, name, '*' || substring(pnum FROM 2 FOR CHAR_LENGTH(pnum)-1 ) from test limit 3;
沒人工作過...
編輯2:
我想到了:
select id, name, '*'::text || substring(pnum FROM 2 FOR CHAR_LENGTH(pnum)-1 ) from test limit 3;
請參見函數regexp_replace(字符串文本,模式文本,替換文本[,標志文本]) 字符串函數和運算符
SELECT regexp_replace('9912333333', '^[9]', '*');
regexp_replace
----------------
*912333333
您可以為此使用Postgres的字符串操作功能。 在您的情況下,“子字符串”和“ Char_Length”
'*' || Substring(<yourfield> FROM 2 FOR CHAR_LENGTH(<yourfield>)-1) as outputfield
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.