所以这是挑战:
我需要按照以下示例更新表格中特定列的字符串:
例1:
来自:texttext / 21/812 / 21a
收件人:texttext-21a81221
例2:
来自:texttext / 6/163 / 38a
收件人:texttext-38a1636
“texttext”长度可能会有所不同,也可能包含斜杠(/)。
此外,数字的块2和块3不能有2位数。
所以,
texttext / 53 / 7A / A2
必须变成:
texttext-0a207a53
我正在使用SQL Server 2008。
感谢您帮助我的努力。
谢谢!
答案 0 :(得分:0)
不确定你对block 2 and block 3 of numbers can’t have 2 digits
的意思
无论如何,下面应该给你一个想法
WITH data AS
(
select Value, REVERSE(Value) AS ReverseValue from (values
('texttext/21/812/21a'), ('texttext/6/163/38a'), ('texttext/53/7a/a2'), ('text/t/e/xt/53/7a/a2')
)t(Value)
), split AS
(
select
Value, ReverseValue,
reverse(substring(ReverseValue, 1, P1.Pos - 1)) AS Forth,
reverse(substring(ReverseValue, P1.Pos + 1, P2.Pos - P1.Pos - 1)) AS Third,
reverse(substring(ReverseValue, P2.Pos + 1, P3.Pos - P2.Pos - 1)) AS Second,
reverse(substring(ReverseValue, p3.Pos + 1, len(ReverseValue))) AS First
from data
cross apply (select (charindex('/', ReverseValue))) as P1(Pos)
cross apply (select (charindex('/', ReverseValue, P1.Pos+1))) as P2(Pos)
cross apply (select (charindex('/', ReverseValue, P2.Pos+1))) as P3(Pos)
)
select Value, First + '-' + Forth + Third + Second AS NewValue from split