sql >> データベース >  >> RDS >> Sqlserver

SQLServerの単一行から複数​​の文字列を抽出する方法

    cteを再帰的に使用して、文字列を取り除くことができます。

    declare @T table (id int, [text] nvarchar(max))
    
    insert into @T values (1, 'Peter (example@sqldat.com) and Marta (example@sqldat.com) are doing fine.')
    insert into @T values (2, 'Nothing special here')
    insert into @T values (3, 'Another email address (example@sqldat.com)')
    
    ;with cte([text], email)
    as
    (
        select
            right([text], len([text]) - charindex(')', [text], 0)),
            substring([text], charindex('(', [text], 0) + 1, charindex(')', [text], 0) - charindex('(', [text], 0) - 1) 
        from @T
        where charindex('(', [text], 0) > 0
        union all
        select
            right([text], len([text]) - charindex(')', [text], 0)),
            substring([text], charindex('(', [text], 0) + 1, charindex(')', [text], 0) - charindex('(', [text], 0) - 1) 
        from cte
        where charindex('(', [text], 0) > 0
    )
    select email
    from cte
    

    結果

    email
    example@sqldat.com
    example@sqldat.com
    example@sqldat.com
    


    1. PostgreSQLで数値をフォーマットする方法

    2. JPQLによるJPAのMap<KEY、VALUE>クエリが失敗しました

    3. mysql-オフセットの問題

    4. SQLServerのLIKE論理演算子とは-SQLServer/TSQLチュートリアルパート123