63

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?

Muhammad Waheed
  • 960
  • 1
  • 11
  • 29
ArK
  • 19,864
  • 65
  • 105
  • 136

8 Answers8

89

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, '%')
MatBailie
  • 77,948
  • 17
  • 98
  • 134
  • 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 09 '09 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 09 '09 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 09 '17 at 21:37
  • @MatBailie is this possible with PostgreSQL in any way at all - the above syntax does not work apparently. – Pravin May 31 '19 at 09:50
30

SQL SERVER

 WHERE ColumnName LIKE '%'+ColumnName+'%'
Yi Jiang
  • 48,053
  • 16
  • 135
  • 134
RoRo
  • 301
  • 3
  • 2
  • 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 '15 at 21:32
7

ORACLE DATABASE example:

select * 
from table1 t1, table2 t2
WHERE t1.a like ('%' || t2.b || '%')
Stephen Docy
  • 4,698
  • 7
  • 17
  • 31
alvaroIdu
  • 393
  • 3
  • 7
6
...
WHERE table1.x LIKE table2.y + '%'
Tomalak
  • 322,446
  • 66
  • 504
  • 612
  • Maybe you need brackets? WHERE table1.x LIKE (table2.y + '%') -- or maybe "||" instead of "+" as per the SQL standard. – Adrian Pronk Sep 09 '09 at 10:21
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+'');
Koekiebox
  • 5,551
  • 13
  • 51
  • 87
0

For SQLLite you will need to concat the strings

select * from list1 l, list2 ll 
WHERE l.name like "%"||ll.alias||"%";
Bhaskar
  • 1,512
  • 1
  • 14
  • 23
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.

Slava Rozhnev
  • 8,085
  • 6
  • 21
  • 35
S Nagendra
  • 11
  • 3
0

This works for me:

SELECT * FROM `mdl_user` WHERE `firstname` LIKE concat(lastname,'%') AND lastname != ''
Elikill58
  • 3,190
  • 22
  • 17
  • 38
M Deril
  • 1
  • 1