how to use LIKE with column name

SqlMysql

Sql Problem Overview


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?

Sql Solutions


Solution 1 - Sql

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, '%')

Solution 2 - Sql

SQL SERVER

 WHERE ColumnName LIKE '%'+ColumnName+'%'

Solution 3 - Sql

ORACLE DATABASE example:

select * 
from table1 t1, table2 t2
WHERE t1.a like ('%' || t2.b || '%')

Solution 4 - Sql

...
WHERE table1.x LIKE table2.y + '%'

Solution 5 - Sql

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+'');

Solution 6 - Sql

For SQLLite you will need to concat the strings

select * from list1 l, list2 ll 
WHERE l.name like "%"||ll.alias||"%";

Solution 7 - Sql

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.

Solution 8 - Sql

This works for me:

SELECT * FROM `mdl_user` WHERE `firstname` LIKE concat(lastname,'%') AND lastname != ''

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArKView Question on Stackoverflow
Solution 1 - SqlMatBailieView Answer on Stackoverflow
Solution 2 - SqlRoRoView Answer on Stackoverflow
Solution 3 - SqlalvaroIduView Answer on Stackoverflow
Solution 4 - SqlTomalakView Answer on Stackoverflow
Solution 5 - SqlKoekieboxView Answer on Stackoverflow
Solution 6 - SqlBhaskarView Answer on Stackoverflow
Solution 7 - SqlS NagendraView Answer on Stackoverflow
Solution 8 - SqlM DerilView Answer on Stackoverflow