tags:

views:

212

answers:

10

I need to write a valid T-SQL query version of the following pseudo-code:

select * from newTable where [name] like in (
    select [name] from oldTable
)

I'm not sure how to go about this. Any help (even directing me to an existing question) would be great. Thanks!

Edit: Per some comments I will clarify this particular case. The tables look like this:

oldTable
code varchar(10)
name varchar(500)

newTable
code varchar(10)
name varchar(500)

In all of the cases where oldTable.code <> newTable.code, I am wanting to see if the oldTable.name is like one of the names in newTable.name. Basically, some of the new names have had qualifiers added to the beginning or end of the names. ie: 'old name' may have a 'qualified old name' in the newTable. Thanks again.

+1  A: 

I think you just need to remove the like eg:

select * from newTable where [Name] in (select name from oldTable)

Mark Redman
A: 

Name like Name? well, you cannot do a LIKE and IN at the same time.

This looks like a good candidate for SOUNDEX

Do a JOIN with a SOUNDEX.

read up here: http://msdn.microsoft.com/en-us/library/aa259235%28SQL.80%29.aspx

Raj More
+1, based on the lack of info in the question, this is the only answer that might work
KM
+6  A: 

Assuming the two tables relate in some way.

SELECT newTable.* FROM newTABLE JOIN oldTable ON <JOIN CRITERIA>
WHERE newTable.[Name] LIKE oldTable.name
Matthew Vines
without using a wildcard it will work the same as =
KM
In my opinion this is the only solution that you should use.
ChaosPandion
A: 

We ran into the same issue ourselves. It may not work for you, but the solution we came up with is: SELECT [Fields]
FROM [Table]
WHERE [Field] like 'Condition1'
OR [Field] like 'Condition2'

Not a great solution, but it works for us.

Tom
+2  A: 

Not so pretty, but it works:

SELECT DISTINCT newTable.*
FROM newTABLE
JOIN oldTable
ON newTable."Name" LIKE oldTable.name

LukLed
+5  A: 
DECLARE @nt TABLE (NAME VARCHAR(10))
DECLARE @ot TABLE (NAME VARCHAR(10))

INSERT INTO @nt VALUES('Stuart')
INSERT INTO @nt VALUES('Ray')


INSERT INTO @ot VALUES('St%')
INSERT INTO @ot VALUES('Stu%')


SELECT *
FROM @nt n
WHERE EXISTS (SELECT *
       FROM @ot o
       WHERE n.name LIKE o.name)
Stuart Ainsworth
+1 Unlike the join answers this will only return one record, although you could use a join and use a select distinct, depending on which one ended performing better.
Yishai
do you really think that the OP is storing a "%" in oldTable.name?? like in your example
KM
No. I have no clue how the OP is storing his data, but he asked for "any help". At least my sample code provided some sample data to work with. :)
Stuart Ainsworth
I forgot to mention that you can always add qualifiers if necessary; e.g: WHERE n.name LIKE '%' + o.name + '%'
Stuart Ainsworth
+1  A: 

Thanks everyone. I used the following query, inspired by both LukLed's answer and a comment by Stuart Ainsworth.

SELECT DISTINCT old.code, old.name, new.name, new.code 
FROM newTable new 
JOIN oldTable old
ON new.name LIKE '%' + old.name + '%' 
WHERE new.code <> old.code
ORDER BY old.name, new.name

Performance isn't that great, but it's a one time analysis and it gets the job done.

The reason I chose this over the "EXISTS" version is because it gives me both results from the new and old tables.

Aaron Palmer
you can probably get rid of the DISTINCT; it isn't buying you much since you're displaying both the old and new columns. The DISTINCT was there in case the new columns had multiple matches on old columns; in this case, if they do, you're still showing them :)
Stuart Ainsworth
A: 

If you use very rarely used cross apply you can do this with ease.
(temp table declaration stolen code from Stuart)

2 tables do not need to have any relationship as Matthews' answer.

DECLARE @nt TABLE (NAME VARCHAR(10))
DECLARE @ot TABLE (NAME VARCHAR(10))

INSERT INTO @nt VALUES('Stuart')
INSERT INTO @nt VALUES('Ray')


INSERT INTO @ot VALUES('St%')
INSERT INTO @ot VALUES('Stu%')

select distinct n.NAME
from @nt n
 cross apply @ot o
where n.NAME like o.name
Sung Meister
Why cross apply versus cross join? Cross join will be available in older versions of SQL Server as well as other RDBMSes. But maybe there is a subtlety I'm missing.
Shannon Severance
A: 

Rather than IN, use an EXISTS subquery. Quick example (SQL Server 2005 and above):

WITH oldTable ([name])
     AS
     (
      SELECT 'One'
      UNION ALL
      SELECT 'Day'
     ),
     newTable ([name])
     AS
     (
      SELECT 'onedaywhen'
      UNION ALL
      SELECT 'Aaron Palmer' 
     )
SELECT N1.[name]
  FROM newTable AS N1
 WHERE EXISTS (
               SELECT * 
                 FROM oldTable AS O1
                WHERE N1.[name] LIKE '%' + O1.[name] + '%'
              );
onedaywhen
A: 

A mere stab in the dark but this is awfully reminiscent of a situation involving non-scalar data. Quick example using csv format (SQL Server 2005 and above):

WITH oldTable ([name])
     AS
     (
      SELECT '003,006,009,012,015'
      UNION ALL
      SELECT '005,015'
     ),
     newTable ([name])
     AS
     (
      SELECT '007'
      UNION ALL
      SELECT '009'
      UNION ALL
      SELECT '015'
     )
SELECT N1.[name]
  FROM newTable AS N1
 WHERE EXISTS (
               SELECT * 
                 FROM oldTable AS O1
                WHERE ',' + O1.[name] + ','
                         LIKE '%,' + N1.[name] + ',%' 
              );
onedaywhen