We help IT Professionals succeed at work.
Get Started

Can't split a string in Sql Server

398 Views
Last Modified: 2014-08-22
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?
Comment
Watch Question
Top Expert 2013
Commented:
This problem has been solved!
Unlock 2 Answers and 6 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE