ORACLE: - 通过删除第一个单词后面的空格来选择名字

时间:2011-10-20 15:20:21

标签: oracle

ORACLE: - 在表中,名字如下

Binda  E                                                                             
Reven   O                                                                               
Drew  J                                            
ANDAL J                                                                                       
WILL Lee                                            
Chad Hardee  

我想以下列格式选择名字

Binda                                                                               
Reven                                                                                  
Drew                                               
ANDAL                                                                                         
WILL                                              
Chad   

我使用以下查询,但没有运气

SELECT  first_name, SUBSTR(first_name, REGEXP_INSTR('first_name','[^ ]+', 1, 1) ) FROM contact.user_names

请建议。

1 个答案:

答案 0 :(得分:6)

NVL(substr(first_name,1,instr(first_name,'')),first_name)

with t AS
    (
    SELECT 'Binda  E   ' as first_name FROM dual                                                                          
    union
    SELECT 'Reven   O  ' as first_name FROM dual                                                                             
    union
    SELECT 'Drew  J    ' as first_name FROM dual                                        
    union
    SELECT 'ANDAL J    ' as first_name FROM dual                                                                                   
    union
    SELECT 'WILL Lee   ' as first_name FROM dual                                         
    union
    SELECT 'Chad Hardee' as first_name FROM dual 
    union
    SELECT 'foobar' as first_name FROM dual 

    )
    SELECT NVL(substr(first_name, 1, instr(first_name,' ')), first_name) FROM t
    ;