Question

Ok I have done extensive research into this, and tried many techniques to no avail.

I have a list of Names, I have a list of Locations. Some Locations have the full Names string contained within them.

I want to pull only the locations which contain the Names somewhere in them, and I do not want to check from a hard-coded list of %names%, needs to be scalable. What i'm working with is below (I don't check for existing temp tables everytime, sry not really a need)

ALSO ALL QUERIES WORK INDIVIDUALLY, except the last one where i finally try to combine

IF OBJECT_ID('tempdb..#ProviderList') IS NOT NULL
DROP TABLE #ProviderList

SELECT distinct
Station
,StaffName --first and last concatenated
,lastName
,ProviderID
--,[ProviderRole]
INTO #ProviderList
FROM --Omited--
Where PrimaryProviderSID in (Select distinct
                            PrimaryProviderSID
                        ----
                        Where PrimaryPosition not like '%zz%'
                            AND PrimaryProviderID > 0)
    AND [TerminationDate] is null
AND (InactivationDate is null)




Select LastName 
INTO #LastName
from #ProviderList


/* Failed experiment
Declare @Names as Table(Name varchar(max))
Insert Into @Names Select LastName 
from #ProviderList

--Select* From @Names

Failed experiment
*/






SELECT distinct [Many fields...],
  LocationName
Into #LocationName
  FROM ------
Where LocationName not like '%z%'
    AND ---filtering criteria to make my list

---------------The part where it comes together

I want to select Everyting from #LocationName but only the records which contain ANY record from the Names list, they will only contain one or none

Select *
From #LocationName  
Where #LastName in (LocationName)?
Where '%' + LastName + '%'  ? --tried concatenation, i want a fuzzy search
Where #LocationName contains(LocationName, #LastName)? ---i know syntax is wrong but this has not proven to be the correct technique anyway
Where Exists(Select LastName from #LastName) --made no change but Not Exists returned nothing...

----------Coworker just brought me this:

 select lastname
 from #LastName
 Where charindex(LastName,
 (SELECT STUFF((SELECT ',' + LocationName
  FROM #LocationName
  FOR XML PATH('')) ,1,1,'') AS Txt )

  )>1

------But that is only going to give us a list of names that are contained in the LocationName field, we also want to pull the Location Name

I know this is a long one, but i hope I was relatively clear about what needs to be done at least if not how I'm doing it.

I feel like I want to make an array and compare each Name value individually to see if it is contained in the LocationName, then if yes add it to a new list. That would be ideal

Was it helpful?

Solution

Give this a try...

CREATE TABLE #NAMES (NAME VARCHAR(100))
CREATE TABLE #LOCATIONS (LOCATIONNAME VARCHAR(100))

INSERT INTO #NAMES 
SELECT 'DAVE' UNION
SELECT 'BOB' UNION 
SELECT 'JIMMY'

INSERT INTO #LOCATIONS 
SELECT 'TODDS BOAT' UNION
SELECT 'MARYS HOUSE' UNION
SELECT 'DAVES HOTTUB' UNION
SELECT 'JIMMYS CRICKEY CLUB'

SELECT L.*
FROM #LOCATIONS L
CROSS APPLY #NAMES N
WHERE CHARINDEX(N.NAME, L.LOCATIONNAME)>0

OTHER TIPS

Maybe this does deserve a separate answer rather than just a comment to JiggsJedi.

It can also be done with a correlated subquery. The execution plan on this sample data is the same either way.

Use the sample data from JiggsJedi.

SELECT L.* 
  FROM #LOCATIONS L 
 WHERE EXISTS (SELECT 1 
                 FROM #NAMES N 
                WHERE CHARINDEX(N.NAME, L.LOCATIONNAME) > 0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top