【发布时间】:2020-09-10 11:08:27
【问题描述】:
我需要提取 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 ║
╚═════════════════════════════════════════════════════════════════════════════════════╩═══════════════════╝
我尝试过使用子字符串和 charindex 的组合:
SELECT text, SUBSTRING( text, CHARINDEX( '][', text) + 2, 11 ) AS Expected_result
但我认为我应该这样做来搜索正确的,并且在找到“][”组合后只得到除最后一个“]”之外的所有内容
这有意义吗?选择右边的表格,直到找到第一个][然后得到除最后一个之外的所有内容]
【问题讨论】:
标签: sql sql-server substring