Roman Schmidt
Roman Schmidt

Reputation: 65

Multilinestring to Linestrings

I'm looking for a way to split MULTILINESTRING into multiple LINESTRINGS using the spatial function of MS SQL. In my table, I have column geometry that makes it easy to get to a string value using the .ToString () function. But I'm looking for the MULTILINESTRING divided into LINESTRINGS function. Do you have any ideas, please?

enter image description here

EDIT:

I have another question, when I have one really long LINESTRING and I want to split them into several shorter LINEs like on picture below is that possible?

enter image description here

Upvotes: 0

Views: 1781

Answers (1)

lptr
lptr

Reputation: 6788

declare @geom geometry = 'MULTILINESTRING((1 1, 5 5), (1 3, 3 1), (-5 3, -8 -2))'; 
select @geom.STGeometryN(n.rownum).ToString() as Linestring
from
(
    select top (@geom.STNumGeometries()) row_number() over(order by @@spid) as rownum
    from sys.all_objects
) as n;

EDIT:

declare @g geometry = 'LINESTRING(1 1, 2 3, 4 8, -6 3, 5 2, -4 15)';

select 
geometry::STLineFromText(concat('LINESTRING(', @g.STPointN(n.rownum).STX, ' ', @g.STPointN(n.rownum).STY, ',', @g.STPointN(n.rownum+1).STX, ' ', @g.STPointN(n.rownum+1).STY, ')'), 0)
from
(
    select top (@g.STNumPoints()-1) row_number() over(order by @@spid) as rownum
    from sys.all_objects
) as n;

Upvotes: 2

Related Questions