4

我有一个字段,其中包含一串定义地理围栏(多边形)的纬度/经度坐标。每个都用逗号分隔。

eg: 'lat,long,lat,long,lat,long'
eg: 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 个逗号中的每一个,或者如果失败,则让正则表达式在 TSQL 中工作

4

5 回答 5

2

您可以使用数字表(顺便说一句,这是一个非常方便的工具)来查找所有逗号的位置,然后仅使用奇数位置数字,在单个 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
;

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

您可以使用此SQL Fiddle 演示来处理查询。

于 2013-10-08T07:01:08.040 回答
2

这结束了我的解决方案:

DECLARE @WorkingCoordList VARCHAR(max) = 'some lat,long,lat,long string'
DECLARE @Processed INT = 0
DECLARE @CommaLoc INT
DECLARE @Count INT = 0

WHILE @Processed = 0        
    BEGIN       
        SET @CommaLoc = PATINDEX('%,%', @WorkingCoordList)  

        IF @Count % 2 = 0   
        BEGIN   
            SET @WorkingCoordList = STUFF(@WorkingCoordList, @CommaLoc, 1, ' ') --Convert comma to space
        END 
        ELSE    
        BEGIN   
            SET @WorkingCoordList = STUFF(@WorkingCoordList, @CommaLoc, 1, '#') -- Convert comma to hash
        END 

        IF @CommaLoc = LEN(@WorkingCoordList)   
        BEGIN   
            SET @WorkingCoordList = LEFT(@WorkingCoordList, LEN(@WorkingCoordList) - 1)  -- trim trailing ,
            SET @WorkingCoordList = RTRIM(LTRIM(REPLACE(@WorkingCoordList, '#', ', '))) -- Convert all the hashes to commas
            SET @Processed = 1
        END 

        SET @Count = @Count + 1 
    END 
END
于 2016-10-13T05:08:34.013 回答
0

我不知道你的正则表达式是如何工作的,但如果你用正则表达式预处理字符串,它可能会与全局搜索一起使用,并像这样替换:

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

于 2013-10-02T23:18:16.047 回答
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

于 2013-10-03T00:00:16.343 回答
0

感谢您的代码:-)

我有一个稍微不同的场景,但是使用“chue x”代码创建了一个函数,在每 3 个 ';' 处添加一个字符 '#'

/*** 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
于 2015-04-08T06:07:37.637 回答