Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract strings between two special characters in TSQL

Tags:

substring

tsql

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

Essentially, I want to extract everything between 1st and 3rd "_"

Thanks a million

like image 454
annettezhang Avatar asked Jun 17 '11 15:06

annettezhang


1 Answers

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
like image 102
Tomalak Avatar answered Oct 12 '22 13:10

Tomalak