用TSQL替换字符串中的每个第二个字符实例

时间:2013-10-02 22:57:52

标签: sql-server regex tsql replace

我有一个字段,其中包含一系列lat / long坐标,用于定义地理围栏(多边形)。每个都用逗号分隔。

例如:'lat,long,lat,long,lat,long'
例如:148.341158,-21.500773,148.341406,-21.504989,148.375136,-21.513174,148.401674,-21.535247,148.418044,-21.532767,148.408867,-21.511685,148.414075,-21.508461,148.36968,-21.432567,148.349094,-21.438768,148.346862, - 21.480187,148.341158,-21.500773,

我想将它与MSSQL中的地理类型(http://msdn.microsoft.com/en-us/library/bb933971.aspx

一起使用
DECLARE @g geography;
SET @g = geography::STPolyFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326);
SELECT @g.ToString();

这似乎需要:'lat long,lat long,lat long'即:对之间没有逗号。

我无法更改源数据,因为它是供应商程序使用的。我需要操纵字符串以删除每2个逗号中的1个,或者失败,在TSQL中使用正则表达式

5 个答案:

答案 0 :(得分:2)

这最终解决了我的问题:

proc

答案 1 :(得分:1)

顺便说一句,您可以使用numbers tablea very handy tool for many purposes)来查找所有逗号的位置,然后,仅使用奇数位置编号,将单个空格替换为相应的逗号SELECT语句。这就是我所说的:

WITH commas AS (
  SELECT number, rn = ROW_NUMBER() OVER (ORDER BY number)
  FROM master.dbo.spt_values
  WHERE type = 'P'
    AND number BETWEEN 1 AND LEN(@coords)
    AND SUBSTRING(@coords, number, 1) = ','
)
SELECT
  @coords = STUFF(@coords, number, 1, ' ')
FROM cte
WHERE rn % 2 = 1
;

在上面的查询中,数字表的“部分”由system table master.dbo.spt_values的子集“播放”。 commas CTE计算@coords字符串中所有逗号的位置,将结果作为行集返回。主SELECT用于赋值语句。它需要number集合中的每个commas,并删除@coords中相应位置的字符,将其替换为空格字符(所有这些都在STUFF函数的帮助下)。

您可以使用此SQL Fiddle demo来播放查询。

答案 2 :(得分:0)

我不知道你的正则表达式是如何工作的,但如果你用正则表达式预处理字符串,它可能适用于全局搜索并替换如下:

找到:,([^,]*(?:,|$))
替换:'$1'即。空间加捕获组1

答案 3 :(得分:0)

您可以像下面这样滚动自己的解析器。它使用字符串中的逗号来查找所有的纬度/经度值。它使用模式将所有值连接在一起:lat long,lat long,...

declare @list varchar(max) 
declare @result varchar(max)
declare @word varchar(max)
declare @splitOn varchar(1)
declare @wpos int
declare @cpos int
declare @wordCount int

select @list = '148.341158,-21.500773,148.341406,-21.504989,148.375136,-21.513174,148.401674,-21.535247,148.418044,-21.532767,148.408867,-21.511685,148.414075,-21.508461,148.36968,-21.432567,148.349094,-21.438768,148.346862,-21.480187,148.341158,-21.500773,'
select @splitOn = ','
select @result = ''

select @cpos = 0
select @wpos = 1
select @wordCount = 1

while (@cpos <= len(@list))
begin
    select @cpos = charindex(@splitOn, @List, @cpos)
    if (@cpos < 1) select @cpos = len(@list) + 1

    select @word = substring(@list, @wpos, @cpos - @wpos)
    select @result = @result + ' ' + @word

    if ((@wordCount % 2) = 0 and (@cpos < len(@list))) select @result = @result + ','

    select @cpos = @cpos + 1
    select @wpos = @cpos
    select @wordCount = @wordCount + 1
end
select @result as result

产生以下字符串:

  

148.341158 -21.500773,148.341406 -21.504989,148.375136 -21.513174,148.401674 -21.535247,148.418044 -21.532767,148.408867 -21.511685,148.414075 -21.508461,148.36968 -21.432567,148.349094 -21.438768,148.346862 -21.480187,148.341158 -21.500773

答案 4 :(得分:0)

感谢您的代码: - )

我有一个稍微不同的场景但是使用&#34; chue x&#34;创建了一个函数。用于添加字符的代码&#39;#&#39;每隔3&#39 ;;&#39;

/*** My select query ***/
SELECT        dbo.fn_AddEveryNthItem(a.MyString, ';','#', 3) AS Expr1
FROM            dbo.MyTable as a

功能在

之下
/*** Function to add character for every nth item ***/
Create function dbo.fn_AddEveryNthItem(@list varchar(1000), @splitOn varchar(1), @addChar varchar(1), @EveryNthItem int) 
RETURNS VARCHAR(1000)
AS
BEGIN
declare @word varchar(max)
declare @result varchar(max) = ''
declare @wpos int = 1
declare @cpos int = 0
declare @wordCount int =1

while (@cpos <= len(@list))
begin
    select @cpos = charindex(@splitOn, @List, @cpos)
    if (@cpos < 1) select @cpos = len(@list) + 1

    select @word = substring(@list, @wpos, @cpos - @wpos)
    select @result = @result + @splitOn + @word

    if ((@wordCount % @EveryNthItem) = 0 and (@cpos < len(@list))) select @result = @result + @addChar

    select @cpos = @cpos + 1
    select @wpos = @cpos
    select @wordCount = @wordCount + 1
end
Return @result
end