71

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?

3
  • From a comment made by Arun, this is for MySQL...
    – MatBailie
    Sep 9, 2009 at 10:45
  • Give him a break, it's only been 40 minutes since he asked! :)
    – MatBailie
    Sep 9, 2009 at 10:50
  • B'day Nora! 11 Questions and no accepts? Cheeky Sod!
    – MatBailie
    Sep 9, 2009 at 16:04

8 Answers 8

99

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, '%')
4
  • i have error as follows ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '+ %' at line 1
    – ArK
    Sep 9, 2009 at 10:19
  • 1
    MySQL doesn't use + as a concatenation operator. Apparently it's a function called concat() with different functions existing for different data types...
    – MatBailie
    Sep 9, 2009 at 10:42
  • 4
    Apreciate you started your answer with "you're close" unlike most arrogant people here who like to discourage pple. tx
    – Mehdi
    Nov 9, 2017 at 21:37
  • @MatBailie is this possible with PostgreSQL in any way at all - the above syntax does not work apparently.
    – Pravin
    May 31, 2019 at 9:50
33

SQL SERVER

 WHERE ColumnName LIKE '%'+ColumnName+'%'
1
  • 10
    Thank you for also answering this in SQL SERVER. Despite @caitriona's comments, people are actually using other DB's out there and may have found this post by using Google
    – whiteshooz
    Oct 21, 2015 at 21:32
9

ORACLE DATABASE example:

select * 
from table1 t1, table2 t2
WHERE t1.a like ('%' || t2.b || '%')
7
...
WHERE table1.x LIKE table2.y + '%'
2
  • Maybe you need brackets? WHERE table1.x LIKE (table2.y + '%') -- or maybe "||" instead of "+" as per the SQL standard. Sep 9, 2009 at 10:21
  • 1
    I tried this but it didn't work. By reading your answer I realized that the table2.y + '%' part had to be on the right side of the LIKE.
    – GuiRitter
    Jan 25, 2023 at 15:54
4
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+'');
1
  • This should optimize the query by narrowing the search from the first table.
    – Koekiebox
    Sep 9, 2009 at 10:18
0

For SQLLite you will need to concat the strings

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

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.

0

This works for me:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.