开发者

How to extract strings between two special characters in TSQL

开发者 https://www.devze.com 2023-03-13 21:55 出处:网络
How to write TSQL script to get \"Monday_Miami\" out of \"Email_Monday_Miami_June\" Essentially, I wan开发者_运维技巧t to extract everything between 1st and 3rd \"_\"

How to write TSQL script to get "Monday_Miami" out of "Email_Monday_Miami_June"

Essentially, I wan开发者_运维技巧t to extract everything between 1st and 3rd "_"

Thanks a million


DECLARE @c varchar(100)
SET     @c = 'Email_Monday_Miami_June' 

SELECT SUBSTRING(
    @c, 
    CHARINDEX('_', @c) + 1, 
    LEN(@c) - CHARINDEX('_', @c) - CHARINDEX('_', REVERSE(@c))
)

returns

Monday_Miami


declare @s varchar(max) = 'Email_Monday_Miami_June'

select parsename(replace(@s, '_', '.'), 3)+'_'+parsename(replace(@s, '_', '.'), 2)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号