Select 最后一组字符之间的子字符串
Select substring between last set of chars from last
我需要提取 2 个字符(“[”和“]”)之间的子字符串,但仅限于最后一次迭代。
此处示例:
╔═════════════════════════════════════════════════════════════════════════════════════╦═══════════════════╗
║ Text ║ Expected result ║
╠═════════════════════════════════════════════════════════════════════════════════════╬═══════════════════╣
║ :OR1[253427815][11 Sep 2020][000685f383][Craft Labor ST][Standard] ║ Standard ║
╠═════════════════════════════════════════════════════════════════════════════════════╬═══════════════════╣
║ :OR1[252348249][11 Sep 2020][0006ff85383][Craft Labor-Allowance][Skill Rate 7] ║ Skill Rate 7 ║
╠═════════════════════════════════════════════════════════════════════════════════════╬═══════════════════╣
║ :OR1[2545528250][11 Sep 2020][00068ff5383][Craft Labor-Allowance][Attendance] ║ Attendance ║
╠═════════════════════════════════════════════════════════════════════════════════════╬═══════════════════╣
║ :OR1[2528454248][11 Sep 2020][000685383][Craft Labor-Allowance][Overtime] ║ Overtime ║
╠═════════════════════════════════════════════════════════════════════════════════════╬═══════════════════╣
║ :OR1[25254548247][11 Sep 2020][000685383][Craft Labor-Allowance][Weather Allowance] ║ Weather Allowance ║
╚═════════════════════════════════════════════════════════════════════════════════════╩═══════════════════╝
我试过结合使用子字符串和字符索引:
SELECT text, SUBSTRING( text, CHARINDEX( '][', text) + 2, 11 ) AS Expected_result
但我认为我应该这样做来搜索正确的并且在找到“][”组合后只得到最后一个“]”以外的所有内容
这有意义吗? select 从右边开始,直到找到第一个 ][ 然后得到除最后一个 ]
之外的所有内容
一种方法是:
select replace(s.value, ']', '')
from t cross apply
string_split(t.text, '[') s
where t.text like concat('%$[', s.value) escape '$';
早期版本的替代方案:
select t.*, replace(replace(v.str, '[', ''), ']', '')
from t cross apply
(values (stuff(t.text, 1, len(t.text) - charindex('[', reverse(t.text)), ''))) v(str);
Here 是一个 db<>fiddle.
试试这个:
SELECT REPLACE(RIGHT([text], CHARINDEX('[]', REVERSE([text]))-1), ']', '') AS Expected_result
我需要提取 2 个字符(“[”和“]”)之间的子字符串,但仅限于最后一次迭代。
此处示例:
╔═════════════════════════════════════════════════════════════════════════════════════╦═══════════════════╗
║ Text ║ Expected result ║
╠═════════════════════════════════════════════════════════════════════════════════════╬═══════════════════╣
║ :OR1[253427815][11 Sep 2020][000685f383][Craft Labor ST][Standard] ║ Standard ║
╠═════════════════════════════════════════════════════════════════════════════════════╬═══════════════════╣
║ :OR1[252348249][11 Sep 2020][0006ff85383][Craft Labor-Allowance][Skill Rate 7] ║ Skill Rate 7 ║
╠═════════════════════════════════════════════════════════════════════════════════════╬═══════════════════╣
║ :OR1[2545528250][11 Sep 2020][00068ff5383][Craft Labor-Allowance][Attendance] ║ Attendance ║
╠═════════════════════════════════════════════════════════════════════════════════════╬═══════════════════╣
║ :OR1[2528454248][11 Sep 2020][000685383][Craft Labor-Allowance][Overtime] ║ Overtime ║
╠═════════════════════════════════════════════════════════════════════════════════════╬═══════════════════╣
║ :OR1[25254548247][11 Sep 2020][000685383][Craft Labor-Allowance][Weather Allowance] ║ Weather Allowance ║
╚═════════════════════════════════════════════════════════════════════════════════════╩═══════════════════╝
我试过结合使用子字符串和字符索引:
SELECT text, SUBSTRING( text, CHARINDEX( '][', text) + 2, 11 ) AS Expected_result
但我认为我应该这样做来搜索正确的并且在找到“][”组合后只得到最后一个“]”以外的所有内容
这有意义吗? select 从右边开始,直到找到第一个 ][ 然后得到除最后一个 ]
之外的所有内容一种方法是:
select replace(s.value, ']', '')
from t cross apply
string_split(t.text, '[') s
where t.text like concat('%$[', s.value) escape '$';
早期版本的替代方案:
select t.*, replace(replace(v.str, '[', ''), ']', '')
from t cross apply
(values (stuff(t.text, 1, len(t.text) - charindex('[', reverse(t.text)), ''))) v(str);
Here 是一个 db<>fiddle.
试试这个:
SELECT REPLACE(RIGHT([text], CHARINDEX('[]', REVERSE([text]))-1), ']', '') AS Expected_result