Solved

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

Posted on 2009-03-31
4
513 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
ID: 24030528
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
ID: 24030586
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
ID: 24088269
Thanks for your help with this.  Your solution worked great...
0
 
LVL 1

Author Closing Comment

by:CraigDerington
ID: 31564847
Excellent.  Thank you.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql query 8 48
constraint check 2 40
TSQL  - Is there any way to return multiple rows on one matching 14 22
Find results from sql within a time span 11 29
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Read about achieving the basic levels of HRIS security in the workplace.
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…

785 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