Selecting a column with keywords from another column of another table - sql

I need help with a probably simple SQL problem. quite new with SQL so not sure how to solve this.
My problem is that i'm trying to select a columnA where the data in the column contains keywords from another columnB in tableB. an example of how the data is as below.
tableA, columnA : {'This apple is red', 'This ball is round', 'This chair is metal'}
tableB, columnB : {'red', 'round'}
Now im thinking something like this query
SELECT columnA FROM tableA
WHERE columnA LIKE '%' + (SELECT columnB FROM tableB) + '%'
So obviously it won't work because the subquery returns more than 1 value. I'm trying to see some other functions to use like CURSOR but I can't figure it out. Help is very appreciated, thank you.

Use EXISTS instead:
SELECT columnA FROM tableA A
WHERE EXISTS (SELECT 1 FROM tableB B where A.columnA LIKE '%' + B.columnB + '%')

You can try this:
select * from tableA A
JOIN tableB B on A.columnA like '%' + B.columnB +'%'

Related

SQL using contains or Like '%%' and IN together

is there any way to write SQL query where we can perform contains Check on a column to a sub query/function
i.e. in this manner
select * from where COLUMN contains IN (select * from func)
or
select * from where '%'+ COLUMN + '%' IN (select * from func)
Try This,
Select * From table_name A
Where Exists (Select * From table_name B
Where B.Column Like '%' + A.Column +'%')
Reviewed my answer because I seem to misunderstood the question
Select a.*
from foo a,
(select * from func) b
where a.id = b.id or a.id like '%'+convert(varchar,b.id)+'%'
This will select records where A result set column id is equal or like %b.id%

Can someone tell me what is the sql expression for below? Result returns true as CD in table A exists partially in value of table B

Can someone tell me what is the sql expression for below? Result returns true because CD in table A exists partially in value of table B.
Table A:
CD
Table B:
ABCD,CD,ABEF
("ABCD,CD,ABEF" is 1 value in the field of table B)
where
TableB.YourField LIKE '%CD%'
OR
where
TableB.YourField LIKE '%' + TableA.TheCDField + '%'
However, VERY poor design of table content when multiple values concatenated in a single field. Should be normalized, but not enough information provided
Assume TableA has Column1 which contains 'CD' and TableB has Column1 which contains 'ABCD,CD,ABEF'. Your text match is in the middle, but can happen at beginning of string, mid string or at the end of the string. So:
select *
from TableA cross join TableB
where
TableB.Column1 like TableA.Column1 +',%' or
TableB.Column1 like '%,'+ TableA.Column1 +',%' or
TableB.Column1 like '%,'+ TableA.Column1

join tables in sql where data does not exactly match up

I am using SQL Server 2008 and I have two tables that I want to join. I have provided something below that shows how my data looks. I want to join the two tables on the given columns, but how can I do this with the "ID" in front of the number in table B? I was thinking of a trim on the join, but I don't know how to do that.
Something like...
Select *
From TableA AS A
Left Join TableB AS B
On A.ColumnA = B.ColumnB
But this won't work because the numbers don't completely match up.
TableA ColumnA
123
456
789
TableB ColumnB
ID123
ID456
ID789
I hope I made this clear enough. Any suggestions?
SQL Fiddle Demo
select *
from tableA a
join tableB b
on 'ID' + cast(columnA as varchar(5)) = b.columnB

SQL select from table where column contains specific strings

I have a table which contains strings which is needed to be searched [like below image]
TableA
After that, I have a table which stores data [like below image]
TableB
And what the search function does is to select from TableB only if the record contains the string in TableA [i.e. The expected result should be like below image(TableC)]
TableC
I've tried using the SQL below, but the SQL had some error while trying to run [Incorrect syntax near 'select'] , also, the SQL is a bit complicated, is there any way to make the SQL simplier?
select * from TableB a
where exists
(select colA from TableB b
where b.colA = a.ColA and Contains (b.ColA, select searchCol from TableA))
Please try:
select
a.*
From tblA a inner join tblB b
on b.Col like '%'+a.Col+'%'
SQL Fiddle Demo
One of these should work according to me
SELECT b.colA
FROM TableB b join TableA a
WHERE colA like '%' + a.ColA + '%';
SELECT b.colA
FROM TableB b join TableA a
WHERE INSTR(b.colA, '{' + a.colA + '}') > 0;
SELECT b.* FROM tblA a,tblB b
WHERE PATINDEX('%'+a.Col+'%',b.Col)>0
SQL FIDDLE DEMO
I have searched for performance difference between PATINDEX and LIKE but not got a conclusive answer , some say PATINDEX is faster when indexing can't be used which is when pattern to be searched is enclosed within wildcard character eg '%'+a.Col+'%' while other post mention they can similar performance .
Perhaps some SO SQL Wizard can look in crystal ball and show us the light

Comparing substrings in same table

I need to run a query that will give me the list of all entries in one column that is NOT LIKE any of the entries in another column, i.e.:
SELECT DISTINCT columnA
FROM tableA
WHERE columnA NOT LIKE (SELECT columnB FROM tableA)
Obviously, the above query doesn't work, I'm providing it only in the hopes that it will clarify what I'm trying to achieve. So, as an example, say that my columns contain the following:
COLUMNA:
ABCD
ABCE
BCDE
BCDF
BCDEF
GHIJ
GHIK
COLUMNB:
ABC
DEF
HIJ
My desired results would be:
BCDE
BCDF
GHIK
There are a total of 396 values in column in the table, so just entering the values manually is not feasible. In addition, as noted in the example, the values in columnB would always be substrings of the values in columnA, so I also need to have my query do the comparison with that in mind.
Thanks in advance for any help anyone can offer, and also apologies if this question has already been answered elsewhere - I did a search but wasn't able to find anything that I could interpret as addressing this specific requirement.
ADDING NEW INFO **
So, as noted, I made a HUGE mistake in that the two columns are in different tables. That said, though, it was easy enough to modify califax's suggestion below as follows:
SELECT DISTINCT COLUMNA
FROM TABLE1 T1
LEFT JOIN TABLE2 T2 ON
T1.COLUMNA LIKE '%' + T2.COLUMNB + '%'
AND T2.COLUMNB IS NULL
However, it's still returning the full list of entries from COLUMNA. I've confirmed that there are entries in COLUMNB that are substrings of the entries in COLUMNA - any ideas why this isn't filtering?
Thanks.
SELECT DISTINCT columnA
FROM tableA as O
WHERE not exists ( select 42 from TableA where O.ColumnA like ColumnB )
Perform a self join, and look for the ones that don't match:
SELECT DISTINCT a1.ColumnA
FROM TableA a1
LEFT JOIN TableA a2
ON a1.ColumnA LIKE '%' + a2.ColumnB + '%'
AND a2.ColumnB IS NULL
(I added a leading wildcard, since you clarified the desired matches in your question.)
UPDATE
If there are two distinct tables, b.ColumnB shows you the ones that don't match:
SELECT DISTINCT a.ColumnA
FROM TableA a
LEFT JOIN TableB b
ON a.ColumnA LIKE '%' + b.ColumnB + '%'
AND b.ColumnB IS NULL
I would try something like :
select distinct columnA from tableA where columnA not like '%' + columnB + '%'
or following criticalfix's remark (as I'm not sure what you wish exactly)
SELECT DISTINCT columnA FROM tableA tbA
WHERE not exists ( select 1 from TableA where tbA.ColumnA like '%' + ColumnB '%' )