Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-03-31
4
Medium Priority
?
524 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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