Reputation: 63
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
Upvotes: 6
Views: 50575
Reputation: 138960
declare @s varchar(max) = 'Email_Monday_Miami_June'
select parsename(replace(@s, '_', '.'), 3)+'_'+parsename(replace(@s, '_', '.'), 2)
Upvotes: 5
Reputation: 338138
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
Upvotes: 21