SQL Matching a string that contains a string - sql

I have two tables in database.
Both tables have a business name column but not always going to be the same.
For example tbl 1 has a business name of 'Aone Dental Practices Limited TA Jaws Dental' and Tbl 2 has a business name of 'Jaws Dental'. I want to be able to join these together as Jaws Dental is visible in both.
I can't seem to get the Like clause working for this.
tried
Tbl1_BusinesName Like '%' + Tbl2_BusinesName + '%'

This query should work :
SELECT *
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.BusinesName LIKE '%'+TS.BusinesName+'%'

Using EXISTS you can get the expected result:
SELECT *
FROM dbo.TableName1 AS Tbl1
WHERE EXISTS (SELECT 1
FROM dbo.TableName2 AS Tbl2
WHERE Tbl1.BusinesName LIKE '%' + Tbl2.BusinesName + '%');

Related

How to write a SQL like query with table column?

How to write a SQL like query with table column ?
I have two tables table1 and table2.
Table1 has Notes
Table2 has billid
We have around 100 bills which satisfies Like condition.
Right now, i am writing with Static value on the column.
How should i write a Like Query by passing Column in the Like Condition.
select * from table1
where Notes like select billid from table2
You are wanting a JOIN here.
If the exact BillID is found in Table1.Column1 then:
select table1.*
from table1
INNER JOIN table2 ON
table1.column1=table2.billid
You could also do this using IN in the WHERE clause if you truly don't need any records from your table2 in the result set:
select *
from table1
where column1 in (SELECT billid FROM table2);
Which is pretty close to what you were aiming for in your attempted query.
Lastly, if you actually mean LIKE which is more of a wildcard match in SQL terms where the column1 merely has to contain a billid then back to the join:
select table1.*
from table1
INNER JOIN table2 ON
table1.column1 LIKE '%' + table2.billid + '%'
Try this. Select a raw from table1 if any match from table2 exists
select *
from table1
where exists (select billid
from table2
where table1.column1 LIKE '%' + table2.billid + '%')

Passing id from one table to another and getting a total union result in SQL Server

I am trying to obtain a result where id is passed from another table in this like expression.
Eg. Id is B001,B002,B003...... B009 in tblname2 and after passing all the id to tblname1, I want the combine result using union:
select *
from tblname1
where id like '%idfromtblname2%'
i.e
select *
from tblname1
where id like '%B001%'
union
select *
from tblname1
where id like '%B002%'
union
select *
from tblname1
where id like '%B003%'
Let me emphasize the reasons why you have a bad design:
A column should represent a single attribute of an entity, not a list of attributes.
Your id represents a foreign key relationship between two entities. You should declare foreign key relationships.
SQL (in general) has poor string manipulation functions.
Your queries cannot make use of indexing or partitions, and the optimizer is basically of no help.
You should have a junction table with one row per table1 id and table2 id. That is the SQLish way of representing such data.
Sometimes, we are stuck with other people's bad design decisions. You can use like in this case:
select t1.*
from tblname1 t1
where ',' + #idfromtable2 + ',' like '%,' + t1.id + ',%';
or using a join directly:
select t1.*
from tblname1 t1 join
table2 t2
on ',' + t2.ids + ',' like '%,' + t1.id + ',%';
Let me offer another version of this - Don't pass a parameter, just use EXISTS() :
SELECT * FROM tblname1 t
WHERE EXISTS(SELECT 1 FROM tblname2 s
WHERE t.id LIKE '%' + s.<Col> + '%')
But, all depends on how you store your data .

Combine LIKE and IN using only WHERE clause

I know this question has been asked, but I have a slightly different flavour of it. I have a use case where the only thing I have control over is the WHERE clause of the query, and I have 2 tables.
Using simple example:
Table1 contains 1 column named "FULLNAME" with hundreds of values
Table2 contains 1 column named "PATTERN" with some matching text
so, What I need to do is select all values from Table 1 which match the values in table 2.
Here's a simple example:
Table1 (FULLNAME)
ANTARCTICA
ANGOLA
AUSTRALIA
AFRICA
INDIA
INDONESIA
Table2 (PATTERN)
AN
IN
Effectively what I need is the entries in Table1 which contain the values from Table2 (result would be ANTARCTICA, ANGOLA, INDIA, INDONESIA)
In other words, what I need is something like:
Select * from Table1 where FULLNAME IN LIKE (Select '%' || Pattern || '%' from
Table2)
The tricky thing here is I only have control over the where clause, I can't control the Select clause at all or add joins since I'm using a product which only allows control over the where clause. I can't use stored procedures either.
Is this possible?
I'm using Oracle as the backend DB
Thanks
One possible approach is to use EXISTS in combination with LIKE in the subquery:
select * from table1 t1
where exists (select null
from table2 t2
where t1.fullname like '%' || t2.pattern || '%');
I believe that you can do this with a simple JOIN:
SELECT DISTINCT
fullname
FROM
Table1 T1
INNER JOIN Table2 T2 ON T1.fullname LIKE '%' || T2.pattern || '%'
The DISTINCT is there for those cases where you might have a match to multiple rows in Table2.
If the patterns are always two characters and only have to match the start of the full name, like the examples you showed, you could do:
Select * from Table1 where substr(FULLNAME, 1, 2) IN (Select Pattern from Table2)
Which prevents any index on Table1 being used, and your real case may need to be more flexible...
Or probably even less efficiently, similar to TomH's approach, but with the join inside a subquery:
Select * from Table1 where FULLNAME IN (
Select t1.FULLNAME from Table1 t1
Join Table2 t2 on t1.FULLNAME like '%'||t2.Pattern||'%')
Right, this involved a bit of trickery. Conceptually what I've done is turned the column from the PATTERN into a single cell, and use that with REGEX_LIKE
So the values "AN and IN" becomes one single value '(AN|IN)' - I just feed this to the regexp_like
SELECT FULLNAME from table1 where
regexp_like(FULLNAME,(SELECT '(' || SUBSTR (SYS_CONNECT_BY_PATH (FULLNAME , '|'), 2) || ')' Table2
FROM (SELECT FULLNAME , ROW_NUMBER () OVER (ORDER BY FULLNAME) rn,
COUNT (*) OVER () cnt
FROM Table2)
WHERE rn = cnt START WITH rn = 1 CONNECT BY rn = PRIOR rn + 1))
The subquery in the regexp_like turns the column into a single cell containing the regular expression string.
I do realise this is probably a performance killer though, but thankfully I'm not that fussed about performance at this point

sql join string match

lets say i have 2 tables
ID Product
----------------
micrsoft sql
cisco
ID Product
----------------
sql
cisco agent
flash
I need to write a query that returns only 'flash' as output becasue it does not have a match on table 1. The words cisco and microsoft have matches on the other table and so needs to be discarded.
This is more of an inner join but using pattern/string match/contains. The strings needs not be an absolute match and can also be a subset . for eg. you have cisco in one table and cisco agent in the other .
/****** Script for SelectTopNRows command from SSMS ******/
SELECT
[Product]
FROM t1 INNER JOIN t2
ON t1.Product= t2.Product
order BY Product
will only satisy those products whose value exactly match . But i would need to query even partial matches
You can use like for this purpose:
SELECT t2.[Product]
FROM t2 LEFT JOIN
t1
ON t1.Product LIKE '%' + t2.Product + '%' OR
t2.Product LIKE '%' + t1.Product + '%'
WHERE t1.Product IS NULL
ORDER BY t2.Product;
Performance will not be good, so I hope you don't have too much data.
Try this:
select Id, Product
from table2 t2
where ID Not IN ( Select IDFrom table1 )

Use a Select Statement within a wildcard where clause

Working in MS SQL 2005 and I want to use a select statement within a wildcard where clause like so:
SELECT text
FROM table_1
WHERE ID LIKE '%SELECT ID FROM table_2%'
I'm looking for product ids within a large body of text that is held in a DB. The SELECT statement in the wildcard clause will return 50+ rows. The statement above is obviously not the way to go. Any suggestions?
You can do a join and construct the like string based on table_2.
SELECT * FROM table_1 t1
INNER JOIN table_2 t2 ON t1.ID LIKE '%' + CONVERT(VARCHAR, t2.ID) + '%'