2016-10-31 269 views
0

我確定這是超級簡單的,但我將如何去將姓氏,名字轉換爲姓氏,FirstInitial?更改姓氏,名字到姓氏,FirstInitial

例如改變史密斯,約翰·史密斯,J或約翰遜,約翰 - 約翰遜,J等

謝謝!

+0

你試過了什麼? –

回答

2

在名字和姓列的情況下:

select LastName,substr(FirstName,1,1) 
from mytable 
; 

在保存在一個列中的全稱的情況下:

select substr(fullname,1,instr(fullname || ',',',')-1) || substr(fullname,instr(fullname || ',',','),2) 
from mytable 
; 

select regexp_replace (fullname,'([^,]*,?)(.).*','\1\2') 
from mytable 
; 
+0

謝謝,這非常有幫助! – Mitch

+0

不客氣。請檢查最後一個版本(第二個選項)。它會保護你,如果沒有全名的逗號 –

+0

@Mitch如果你滿意的話,不要忘記接受這個答案! –

0

下面是一個方法,僅使用「標準」instrsubstr。假設你輸入一個字符串格式'Smith,John'

select substr(fullname, 1, instr(fullname, ',')+1) from yourtable; 

yourtable是表的名稱,fullname是列名。

instr(fullname, ',') finds the position of the comma within the input string (it would be 6 in 'Smith,John'); then substr instr`,PLUS 1(獲得第一個首字母)。