2012-01-19 79 views
0

我有一個字段,其中包含帶有需要提取到另一列的id號的混合數據。我希望從中提取的列有一些匹配格式'姓氏,名字-ID'的記錄。我只想剝去'身份證'部分,並從那些有' - '和後面有數字的專欄中刪除。從PostgreSQL中的varchar中提取id號

所以我試圖做的是...

update data.xml_customerqueryrs 
set new_id = regexp_replace(name, '[a-z]A-Z]', '') 
where name like '%-%'; 

我知道有一些輕微的,我需要解決,但我不知道作爲模式匹配PostgreSQL文檔並沒有真正做好覆蓋僅搜索數字的工作。

+0

咄... 更新data.xml_customerqueryrs 組NEW_ID =串(姓名,strpos(名稱, ' - '),長度(姓名) ) 其中名稱如'% - %'; – MISMajorDeveloperAnyways

回答

1

或者你也可以這樣做:

new_id = substr(name, strpos(name, '-'), length(name)) 

編號:Strings

+0

在結果中包含' - '。此外,請不要鏈接到手冊的古老版本8.0,因爲Google無法直接得出結果。我修好了它。考慮[this](http://meta.stackexchange.com/questions/108714/best-way-to-reference-the-postgresql-manual)。 –

3

如果你真的only want to strip the 'ID' part

new_id = regexp_replace(name, '-.*?$', '') 

或者,如果你,其實想提取的ID部分:

new_id = substring(name, '-(.*?)$') 

我使用*? quantifier,以便只提取最後一部分,其中名稱中包含-。像:

Skeet-Gravell,John-1234 

String functions當前手冊