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%
Related
The goal is to not show duplicate with slight differences. Here is an example:
I want to remove what is in green
This is the query I am currently using, but not quite accurate.
SELECT Package
FROM myTable
WHERE Package NOT IN (SELECT Package FROM myTable WHERE Package NOT LIKE '%_AC')
With just your sample data to go on you could try using a not exists criteria, such as
select *
from t
where not exists (
select * from t t2
where t.package like Concat(t2.package,'%')
and t.package != t2.package
);
A couple of ideas. If you have a unique column, you could use a NOT EXISTS:
SELECT P
FROM dbo.YourTable YT
WHERE NOT EXISTS (SELECT 1
FROM dbo.YourTable e
WHERE (YT.P LIKE e.P + '%'
OR e.P LIKE YT.P + '%')
AND YT.ID != e.ID);
If not, then you could use a CTE and a windowed COUNT:
WITH CTE AS(
SELECT YT1.*,
COUNT(YT2.P) OVER (PARTITION BY YT1.P) AS C
FROM dbo.YourTable YT1
JOIN dbo.YourTable YT2 ON YT1.P LIKE YT2.P + '%'
OR YT2.P LIKE YT1.P + '%') --Will always JOIN to itself
SELECT P
FROM CTE
WHERE C = 1;
db<>fiddle
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 +'%'
I have Table_A that has a column: 'FullName',columnB, .. and other columns
I have Table_B that has columns: 'FirstName', 'LastName','Job_OID'
Evidently, If we join the characters of FirstName and LastName, it matches Full_Name.
This is the only thing common between these two tables!
Problem: When the Full Name is "John Smith" in Table_A, I want to fetch his Job_OID from table_B.
In simple language,
select job_oid from table_B where ('FirstName' + 'LastName') = Table_A.FullName;
try this:
SELECT job_oid
FROM table_B
JOIN Table_A ON Table_A.FullName = Table_B.FirstName+' '+TableB.LastName
You can also write query in this way:
SELECT B.job_oid
FROM Table_A A
INNER JOIN table_B B ON A.FullName = CONCAT(B.FirstName, ' ', B.LastName)
I think, you must normalize your DB, but if it isn't possible, you can write following query:
SELECT B.*
FROM Table_B B
WHERE EXISTS
(SELECT 'PERSON'
FROM Table_A A
WHERE B.FirstName + ' ' + B.LastName = A.FullName)
You can use, otherwise, JOIN command like this:
SELECT B.*
FROM Table_B B
JOIN Table_A A
ON B.FirstName + ' ' + B.LastName = A.FullName
So, in this way you link two tables with a concatenation of two fields (FirstName and LastName of Table_B) with FullName stored in Table_A
I wrote this but it only returns exact matchs such as 'Carburetor' not 'Brand X Carburetor'
Any help would be greatly appreciated!
SELECT [Col]
FROM a
WHERE ([Col]) IN
( SELECT [col]
FROM B
)
UNION ALL
SELECT Distinct [col]
FROM B
WHERE ([col]) IN
(
Select [col]
FROM A
)
Using SQL Server, you might try as follow.
SELECT a.[Col]
FROM a
INNER JOIN b ON a.Col LIKE '%' + b.Col + '%'
UNION ALL
SELECT Distinct b.[col]
FROM b
INNER JOIN a ON b.COL LIKE '%' + a.Col + '%'
Normally LIKE statement is used to check the pattern like data.
example:
select * from table1 where name like 'ar%'
My problem is to use one column of table with LIKE statement.
example:
select * from table1, table2 where table1.x is like table2.y%
Query above results error . how to use one column data in like query?
You're close.
The LIKE operator works with strings (CHAR, NVARCHAR, etc). so you need to concattenate the '%' symbol to the string...
MS SQL Server:
SELECT * FROM table1,table2 WHERE table1.x LIKE table2.y + '%'
Use of LIKE, however, is often slower than other operations. It's useful, powerful, flexible, but has performance considerations. I'll leave those for another topic though :)
EDIT:
I don't use MySQL, but this may work...
SELECT * FROM table1,table2 WHERE table1.x LIKE CONCAT(table2.y, '%')
SQL SERVER
WHERE ColumnName LIKE '%'+ColumnName+'%'
ORACLE DATABASE example:
select *
from table1 t1, table2 t2
WHERE t1.a like ('%' || t2.b || '%')
...
WHERE table1.x LIKE table2.y + '%'
declare #LkeVal as Varchar(100)
declare #LkeSelect Varchar(100)
Set #LkeSelect = (select top 1 <column> from <table> where <column> = 'value')
Set #LkeVal = '%' + #LkeSelect
select * from <table2> where <column2> like(''+#LkeVal+'');
For SQLLite you will need to concat the strings
select * from list1 l, list2 ll
WHERE l.name like "%"||ll.alias||"%";
for MySql you use like below,which is worked for me
SELECT * FROM table1, table2 WHERE table1.x LIKE (table2.y);
It takes table2's column values of y.
This works for me:
SELECT * FROM `mdl_user` WHERE `firstname` LIKE concat(lastname,'%') AND lastname != ''