SQL用分号替换所有其他逗号

时间:2014-02-24 18:26:57

标签: sql sql-server replace

我有一堆字符串应该存储为值对但不是。现在我需要用分号替换所有其他逗号以使它们成对。希望找到一种简单的方法,但可能没有。

前:

  

-1328.89,6354.22,-1283.94,6242.96,-1172.68,6287.91,-1217.63,6399.18

应该是:

  

-1328.89,6354.22; -1283.94,6242.96; -1172.68,6287.91; -1217.63,6399.18

4 个答案:

答案 0 :(得分:1)

create function f_tst(@a varchar(100)) -- use right size of field
returns varchar(100) -- make sure you use the right size of field
begin
declare @pos int = charindex(',', @a) + 1
;while 0 < charindex(',', @a, @pos) 
select @a = stuff(@a, charindex(',', @a, @pos), 1, ';'), 
@pos = charindex(',', @a, charindex(',', @a, @pos + 1)) + 1

return @a
end

go

declare @a varchar(100) = '-1328.89,6354.22,-1283.94,6242.96,-1172.68,6287.91,-1217.63,6399.18'
select dbo.f_tst(@a)

或者在你的例子中

update <table>
set <field> = dbo.f_tst(<field>)

答案 1 :(得分:0)

肯定不是你想要的那么简单,而是一个CHARINDEX / SUBSTRING解决方案:

Declare @input nvarchar(max) = '-1328.89,6354.22,-1283.94,6242.96,-1172.68,6287.91,-1217.63,6399.18'

Declare @i int = 0, @t int = 0, @isComma bit = 1
Declare @output nvarchar(max) = ''
Select @i = CHARINDEX(',', @input)
While (@i > 0)
Begin
    Select @output = @output + SUBSTRING(@input, @t + 1, @i - @t - 1) + CASE @isComma WHEN 1 THEN ',' ELSE ';' END
    Select @t = @i
    Select @i = CHARINDEX(',', @input, @i + 1), @isComma = 1 - @isComma
End
Select @output = @output + SUBSTRING(@input, @t + 1, 1000)
Select @output

答案 2 :(得分:0)

这可以通过动态sql和xml的组合来完成:

declare @sql nvarchar(max)
set @sql = '-1328.89,6354.22,-1283.94,6242.96,-1172.68,6287.91,-1217.63,6399.18'
set @sql = '
    select replace((select cast(value as varchar(50)) +
    cast(case row_number() over(order by sort)%2 when 0 then '','' else '';'' end as char(1))
    from (select ' + replace(@sql,',',' value,1 sort union all select ') + ',1 sort)q
    for xml path(''''))+''||'','',||'','''') YourUpdatedValue'
exec(@sql)

答案 3 :(得分:0)

这可以在一个查询中完成:

DECLARE @t TABLE (id int, col varchar(max))
INSERT @t VALUES
(1,'-1328.89,6354.22,-1283.94,6242.96,-1172.68,6287.91,-1217.63,6399.18'),
(2,'-4534.89,454.22,-1123.94,2932.96,-1872.68,327.91,-417.63,635.18')

;WITH t AS (
  SELECT id, i % 2 x, i / 2 y, val
  FROM @t
  CROSS APPLY (SELECT CAST('<a>'+REPLACE(col,',','</a><a>')+'</a>' AS xml) xml1 ) t1
  CROSS APPLY (
    SELECT
      n.value('for $i in . return count(../*[. << $i])', 'int') i,
      n.value('.','varchar(max)') AS val
    FROM xml1.nodes('a') x(n)
  ) t2
)
SELECT id, y, [0]+','+[1] col
FROM t
PIVOT(MAX([val]) FOR x IN ([0],[1])) t3
ORDER BY id, y


id  y   val
----------------------------
1   0   -1328.89,6354.22
1   1   -1283.94,6242.96
1   2   -1172.68,6287.91
1   3   -1217.63,6399.18
2   0   -4534.89,454.22
2   1   -1123.94,2932.96
2   2   -1872.68,327.91
2   3   -417.63,635.18