简体   繁体   中英

Changing LastName,FirstName to LastName,FirstInitial

I'm sure this is super easy, but how would I go about converting LastName,FirstName to LastName,FirstInitial?

For example changing Smith,John to Smith,J or Johnson,John to Johnson,J etc.

Thank You!

In case of LastName and FirstName columns:

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

In case of a fullname saved in a single column:

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

or

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

Here is one way, using just "standard" instr and substr . Assuming your input is a single string in the format 'Smith,John' :

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

yourtable is the name of the table, and fullname is the name of the column.

instr(fullname, ',') finds the position of the comma within the input string (it would be 6 in 'Smith,John'); then instr(fullname, ',') finds the position of the comma within the input string (it would be 6 in 'Smith,John'); then substr takes the substring that begins at the first position (the 1 in the function call) and ends at the position calculated by instr`, PLUS 1 (to get the first initial as well).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM