[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

Can't split a string in Sql Server

There is no shortage of split string functions on the net for Sql Server.  I have tried 6 of them and I get the same result.  First of all, I'm using a function with spatial methods to retrieve the 7.5' quadrangles intersected by a line...
CREATE FUNCTION [dbo].[GetTrackQuads2] 
(
	-- Add the parameters for the function here
	@trkID int
)
RETURNS varchar(500) AS

BEGIN
DECLARE @QuadGeo GEOGRAPHY
SET @QuadGeo = (SELECT track_geog FROM track_aggregate WHERE trackID = @trkID)

DECLARE @StringList varchar(500) 
SELECT @StringList = COALESCE(@StringList + ', ', '') + RTRIM(tblQuad.Map_Name) 

FROM tblQuad

WHERE quad_geog.STIntersects(
	@QuadGeo	
) = 1


IF @StringList IS NULL 
SET @StringList = 'No Quad'
RETURN @StringList 
END

Open in new window


A trigger calls the function and creates, for example, 'ADONA, ATKINS' in the trkQuads column's row.  Sometimes it's just one quad, 'ADONA'.   When I try to use a split string function I'm only returning 'A'.
CREATE FUNCTION [dbo].[SplitStrings_CTE]
    (
       @List       NVARCHAR(MAX),
       @Delimiter  NVARCHAR(255)
    )
    RETURNS @Items TABLE (Item NVARCHAR(4000))
    WITH SCHEMABINDING
    AS
    BEGIN
       DECLARE @ll INT = LEN(@List) + 1, @ld INT = LEN(@Delimiter);
     
       WITH a AS
       (
           SELECT
               [start] = 1,
               [end]   = COALESCE(NULLIF(CHARINDEX(@Delimiter, 
                           @List, 1), 0), @ll),
               [value] = SUBSTRING(@List, 1, 
                         COALESCE(NULLIF(CHARINDEX(@Delimiter, 
                           @List, 1), 0), @ll) - 1)
           UNION ALL
           SELECT
               [start] = CONVERT(INT, [end]) + @ld,
               [end]   = COALESCE(NULLIF(CHARINDEX(@Delimiter, 
                           @List, [end] + @ld), 0), @ll),
               [value] = SUBSTRING(@List, [end] + @ld, 
                         COALESCE(NULLIF(CHARINDEX(@Delimiter, 
                           @List, [end] + @ld), 0), @ll)-[end]-@ld)
           FROM a
           WHERE [end] < @ll
       )
       INSERT @Items SELECT [value]
       FROM a
       WHERE LEN([value]) > 0
       OPTION (MAXRECURSION 0);
     
       RETURN;
    END

Open in new window

USE GPSTrackLogs
DECLARE @TrkQuads NVARCHAR
SET @TrkQuads = (SELECT trkQuads FROM track_aggregate WHERE trackID = 4)
SELECT * FROM dbo.SplitStrings_CTE(
	@TrkQuads, ','
	)

Open in new window


What am I doing wrong?
0
Nate_LR
Asked:
Nate_LR
2 Solutions
 
Scott PletcherSenior DBACommented:
I know of a really good splitter, but it doesn't make a lot of sense to concatenate them just to turn around and split them.

Perhaps, instead, make the first function a table-valued function that returns the individual values you need:

USE GPSTrackLogs


CREATE FUNCTION [dbo].[GetTrackQuads_Table]
(
      @trkID int
)
RETURNS TABLE
AS
RETURN (
    SELECT RTRIM(tblQuad.Map_Name) AS Map_Name
    FROM (
        SELECT track_geog AS QuadGeo
        FROM track_aggregate
        WHERE trackID = @trkID
    ) AS t_agg
    INNER JOIN tblQuad ON
        quad_geog.STIntersects(t_agg.QuadGeo) = 1
)


SELECT *
FROM [dbo].[GetTrackQuads_Table] ( 4 )
0
 
chaauCommented:
I cannot believe how easy it is to fix. have a look at this code:
USE GPSTrackLogs
DECLARE @TrkQuads NVARCHAR(MAX)
SET @TrkQuads = (SELECT trkQuads FROM track_aggregate WHERE trackID = 4)
SELECT * FROM dbo.SplitStrings_CTE(
	@TrkQuads, ','
	)

Open in new window


Have a look how I have declared the @TrkQuads variable.
Read this blog about bad habits of declaring (N)VARCHAR without size
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Chaau is right. You need to set MAX value for NVARCHAR or else will return only a character.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Nate_LRAuthor Commented:
I'm happy this was easy for someone out there.  Thanks for the solutions!
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
I don't think it's a question on being easy or hard to resolve but sometimes we got so addicted in the code that the best thing to do it's to ask for someone to see it for the first time. Another pair of eyes and a different way of thinking helps a lot :)
0
 
Nate_LRAuthor Commented:
I agree Vitor, thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now