Solved

SQL - Find Records Where Values Match Characters in Comma Delimited String

Posted on 2009-03-31
4
512 Views
Last Modified: 2012-05-06
I am using a PATINDEX in a query to find values within a comma delimited string.  However, if a string is holding values (Z, A), the query only returns those records where both Z and A are contained in the string and in that order.  What I need is a SQL function that would find records that contain both the Z and the A (Z , A) or A and Z (A , Z) or only Z or only A.  The wild cards % does not seems to help find records where the code is a single letter and does not meet both criteria.


DECLARE @long1 decimal(5,2); 

DECLARE @lat1 decimal(5,2);

DECLARE @rangeFactor decimal(7,6);

SET @rangeFactor = 0.014457;

SELECT @lat1 = LATITUDE, @long1 = LONGITUDE

FROM dbo.zipcodes where zipcode = '" + Replace(rsC9__varZ2, "'", "''") + "';

SELECT DISTINCT(C.CommunityName), B.ABBR, B.Latitude, B.Longitude, B.zipcode, C.CommunityID, C.CommCity, C.CommState, C.CommZip, CommCode1         

FROM dbo.zipcodes B inner join dbo.Communities C ON B.ZipCode = C.CommZip

WHERE B.LATITUDE BETWEEN @lat1-(100 *@rangeFactor) and @lat1+(100 *@rangeFactor)

AND B.LONGITUDE BETWEEN @long1-(100 *@rangeFactor) and @long1+(100 *@rangeFactor)

AND dbo.getDistance(@lat1,@long1,B.latitude,B.longitude) <= 100

AND (PATINDEX('%" + Replace(rsC9__varS4, "'", "''") + "%', CommCode1) > 0)

ORDER BY C.CommunityName ASC"

Open in new window

0
Comment
Question by:CraigDerington
  • 2
4 Comments
 
LVL 25

Expert Comment

by:reb73
Comment Utility
Create an UDF that parses delimited strings to a table like the one in the link below -

http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_22819980.html?sfQueryTermInfo=1+10+dbo.fnsplit

Then just do an inner join on this UDF like the template SQL code below (code from line 7 to 12 in your code being replaced with the code below)
SELECT DISTINCT(C.CommunityName), B.ABBR, B.Latitude, B.Longitude, B.zipcode, C.CommunityID, C.CommCity, C.CommState, C.CommZip, CommCode1         

FROM dbo.zipcodes B inner join dbo.Communities C ON B.ZipCode = C.CommZip

inner join dbo.fnsplit(' + REPLACE(rsC9__varS4, "'", "''") + ', ',') F ON C.CommCode1 LIKE ''%'' + F.SplitCol + ''%''

WHERE B.LATITUDE BETWEEN @lat1-(100 *@rangeFactor) and @lat1+(100 *@rangeFactor)

AND B.LONGITUDE BETWEEN @long1-(100 *@rangeFactor) and @long1+(100 *@rangeFactor)

AND dbo.getDistance(@lat1,@long1,B.latitude,B.longitude) <= 100

ORDER BY C.CommunityName ASC"

Open in new window

0
 
LVL 22

Accepted Solution

by:
8080_Diver earned 500 total points
Comment Utility
The provided code looks somewhat like a stored procedure or a portion thereof.  What you need to do is to break the comma separated list of characters into something that will work (perhaps an in-memory table) and then build your SQL query so that it makes use of that something.  
I am assuming that rsC9__varS4 is an input variable to the stored procedure.  So, the following code should be something like what you will need.
By the way, DISTINCT doesn't work on a column by column basis but on a row by row basis, so you DISTINCT(C.CommunityName) isn't really correct syntax. ;-)
 

DECLARE @long1 decimal(5,2); 

DECLARE @lat1 decimal(5,2);

DECLARE @rangeFactor decimal(7,6);

SET @rangeFactor = 0.014457;

SELECT @lat1 = LATITUDE, 

       @long1 = LONGITUDE

FROM   dbo.zipcodes 

where zipcode = '" + Replace(rsC9__varZ2, "'", "''") + "';
 

DECLARE @TempListTable TABLE

                       (

                        ACharacter AS VarChar(10)

                       );
 

DECLARE @TempChars AS VarChar(10);

DECLARE @WorkString AS VarChar(8000);

DECLARE @CommaPos AS INT;
 

-- Set the WorkString variable to an empty string

SET @WorkString = '';
 

-- Find the position of the first comma, if any

SET @CommaPos	=	CharIndex(',', rsC9__varS4);
 

IF (@CommaPos > 0)

THEN

BEGIN

-- We got a comma, so add the first string and set the WorkString to the rest

	INSERT LEFT(rsC9__varS4, @CommaPos - 1) INTO @TempListTable ;

	SET @WorkString = RIGHT(rsC9__varS4, LENGTH(rsC9__varS4) - @CommaPos);

END
 

WHILE (@WorkString <> '')

BEGIN

	SET @CommaPos	=	CharIndex(',', @WorkString);
 

	IF (@CommaPos > 0)

	THEN

	BEGIN

	-- We got a comma, so add the first string and set the WorkString to the rest

		INSERT LEFT(@WorkString, @CommaPos - 1) INTO @TempListTable ;

		SET @WorkString = RIGHT(@WorkString, LENGTH(@WorkString) - @CommaPos);

	END
 

END

 
 

SELECT DISTINCT 

       C.CommunityName, 

       B.ABBR, 

       B.Latitude, 

       B.Longitude, 

       B.zipcode, 

       C.CommunityID, 

       C.CommCity, 

       C.CommState, 

       C.CommZip, 

       CommCode1         

FROM   dbo.zipcodes B 

inner join dbo.Communities C 

  ON   B.ZipCode = C.CommZip

JOIN   @TempListTable  Z

WHERE  B.LATITUDE BETWEEN @lat1-(100 *@rangeFactor) and @lat1+(100 *@rangeFactor)

  AND B.LONGITUDE BETWEEN @long1-(100 *@rangeFactor) and @long1+(100 *@rangeFactor)

  AND dbo.getDistance(@lat1,@long1,B.latitude,B.longitude) <= 100

  AND (PATINDEX('%' + Z.ACharacter  + '%', CommCode1) > 0)

ORDER BY C.CommunityName ASC"

Open in new window

0
 
LVL 1

Author Comment

by:CraigDerington
Comment Utility
Thanks for your help with this.  Your solution worked great...
0
 
LVL 1

Author Closing Comment

by:CraigDerington
Comment Utility
Excellent.  Thank you.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now