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?
Nate_LRAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
Chaau is right. You need to set MAX value for NVARCHAR or else will return only a character.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.