这是我的查询
select
custid,
Stuff(Coalesce('' + t12, '') + Coalesce('' + t11, '') + Coalesce('' + t10, '') +
Coalesce('' + t9, '') + Coalesce('' + t8, '') + Coalesce('' + t7, '') +
Coalesce('' + t6, '') + Coalesce('' + t5, '') + Coalesce('' + t4, '') +
Coalesce('' + t3, '') + Coalesce('' + t2, '') + Coalesce('' + t1, '') +
Coalesce('' + id, ''), 1, 0, '') AS path
from
Table
这是我回来的结果:
431222294701031547005760100001411000302910000718.
需要这样:
43122229/47010315/47005760/10000141/10003029/10000718
如果我使用
SELECT columns, stuff(Coalesce('/' + t2, '') + Coalesce('/' + t1, '') , 1, 0, '') AS path
FROM table
结果如下所示:
/43122229/47010315/47005760/10000141/10003029/10000718.
如何解决此问题?请帮助我几个小时以来一直挠头。...
答案 0 :(得分:0)
如果知道字符串的最大长度,则可以使用CROSS APPLY
提取每8个字符,然后在它们之间用“ /”连接
declare @sample table
(
col varchar(100)
)
insert into @sample (col) select '12345678901234567890123'
insert into @sample (col) select '123456789012345678901234567890'
insert into @sample (col) select '12'
select s.col,
new_col = isnull(col1, '')
+ isnull('/' + col2, '')
+ isnull('/' + col3, '')
+ isnull('/' + col4, '')
+ isnull('/' + col5, '')
from @sample s
cross apply
(
select col1 = nullif(substring(col, 1, 8), '')
) p1
cross apply
(
select col2 = nullif(substring(col, 9, 8), '')
) p2
cross apply
(
select col3 = nullif(substring(col, 17, 8), '')
) p3
cross apply
(
select col4 = nullif(substring(col, 25, 8), '')
) p4
cross apply
(
select col5 = nullif(substring(col, 33, 8), '')
) p5
答案 1 :(得分:0)
在下面的查询中尝试此
DECLARE @txt varchar(max)='/43122229/47010315/47005760/10000141/10003029/10000718'
SELECT STUFF(@txt,1,1,'') as newText
示例:
1
这表示从第一个1
字符位置开始,将id=xxx
字符替换为”