如何编写TSQL脚本以从“Email_Monday_Miami_June”中获取“Monday_Miami”
基本上,我想提取第1和第3个“_”
之间的所有内容万分感谢
答案 0 :(得分:19)
DECLARE @c varchar(100)
SET @c = 'Email_Monday_Miami_June'
SELECT SUBSTRING(
@c,
CHARINDEX('_', @c) + 1,
LEN(@c) - CHARINDEX('_', @c) - CHARINDEX('_', REVERSE(@c))
)
返回
Monday_Miami
答案 1 :(得分:5)
declare @s varchar(max) = 'Email_Monday_Miami_June'
select parsename(replace(@s, '_', '.'), 3)+'_'+parsename(replace(@s, '_', '.'), 2)