Split string in database row and copy results to different rows - SQL Server

前端 未结 3 1622
暗喜
暗喜 2021-01-16 14:58

I created an application to send mail to users. I\'m using a database where some users have more than one email address assigned.

Name                                


        
3条回答
  •  广开言路
    2021-01-16 15:28

    In Oracle

    Here is a mock for provided example:

    select 'BusinessXPTO' name, 'mail1@xpto.com;mail2@xpto.com' mail from dual;
    

    Here is how to split the mail column into separate rows

    select 'BusinessXPTO' name, regexp_substr('mail_1@xpto.com;mail_2@xpto.com','[^;]+',1, rn) mail  
    from dual 
    cross join 
        (select rownum rn from 
            (select max(length(regexp_replace('mail_1@xpto.com;mail_2@xpto.com', '[^;]+'))) + 1 mx from dual) 
        connect by level <= mx ) 
    where regexp_substr ('mail1@xpto.com;mail2@xpto.com', '[^;]+', 1, rn) is not null 
    order by rn;
    

    Here is the result

    NAME            MAIL
    BusinessXPTO    mail_1@xpto.com
    BusinessXPTO    mail_2@xpto.com
    

提交回复
热议问题