How to extract this specific substring in SQL Server?

前端 未结 5 2276
慢半拍i
慢半拍i 2020-12-03 18:04

I have a string with a specific pattern:

23;chair,red [$3]

i.e., a number followed by a semicolon, then a name followed by a left square br

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 18:33

    If you need to split something into 3 pieces, such as an email address and you don't know the length of the middle part, try this (I just ran this on sqlserver 2012 so I know it works):

    SELECT top 2000 
        emailaddr_ as email,
        SUBSTRING(emailaddr_, 1,CHARINDEX('@',emailaddr_) -1) as username,
        SUBSTRING(emailaddr_, CHARINDEX('@',emailaddr_)+1, (LEN(emailaddr_) - charindex('@',emailaddr_) - charindex('.',reverse(emailaddr_)) ))  domain 
    FROM 
        emailTable
    WHERE 
        charindex('@',emailaddr_)>0 
        AND 
        charindex('.',emailaddr_)>0;
    GO
    

    Hope this helps.

提交回复
热议问题