:如何将列的单个值拆分为两个并返回值和长度

时间:2015-07-20 12:48:14

标签: sql oracle

我有一张桌子'测试'名字,姓氏列中的姓氏。如何拆分名字和姓氏,还要返回名字和姓氏的长度。 例如:

name
hello world
Thomas Edison
Christopher Columbus

我希望结果为:

First_name    FirstName_length    last_name    LastName_length
hello          5                   world       5 
Thomas         6                   Edison      6
Christopher    11                  Columbus    8

3 个答案:

答案 0 :(得分:1)

select split_part(name,' ',1) as first_name,
 length(split_part(name,' ',1)) as firstname_length,
 split_part(nme,' ',2) as last_name, 
 length(split_part(name,' ',2)) as lasttname_length from table_name 

答案 1 :(得分:1)

SQLFIDDLE ..同样的

只需将table_name和column_name替换为实际名称

即可
select substr("name", 1, instr("name", ' ')-1) first_name,
       length(substr("name", 1, instr("name", ' ')-1)) First_Name_length ,
       substr("name",instr("name", ' ')+1) Last_Name,
       length(substr("name",instr("name", ' ')+1)) Last_name_length
from table_name

O / P

FIRST_NAME  First_LENGTH    LAST_NAME   LAST_LENGTH
hello            5           world      5
Thomas           6           Edison     6
Christopher     11           Columbus   8

答案 2 :(得分:0)

 select First_name, length(First_name), Last_name, length(Last_name) from test

SQL Fiddle