tags:

views:

127

answers:

3

Normally LIKE statement used to check the pattern like data.

ex:

select * from table1 where name like 'ar%'

My problem is to use one column of table with LIKE statement.

ex:

select * from table1, table2 where table1.x is like table2.y%

Query above creates error creates . how to use one column data in like query?

+2  A: 
...
WHERE table1.x LIKE table2.y + '%'
Tomalak
i'm using mysql , its error responce for me!!
Paniyar
Maybe you need brackets? WHERE table1.x LIKE (table2.y + '%') -- or maybe "||" instead of "+" as per the SQL standard.
Adrian Pronk
+5  A: 

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, '%')
Dems
i have error as followsERROR 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
Paniyar
MySQL doesn't use + as a concatenation operator. Apparently it's a function called concat() with different functions existing for different data types...
Dems
A: 
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
This should optimize the query by narrowing the search from the first table.
Koekiebox