I am aware of using where and like this
select * from Table1
join table2
on table1.column like %text%
i want to extend the same logic to the column. i.e
select * from Table1
join table2
on table1.column like %table2.column%
This is impossible on SQL? is there an alternative for this?
Thanks
Try to get into the habit of always specifying the DB engine you are using and including sample data and expected output based on that sample data. In this case the sample data etc can help us provide a more relevant answer. there may be a better solution to your problem than using like in your join clause.
but for the simple example you have given the below should work.
select * from Table1
join table2
on table1.column like '%' + table2.column + '%'
EDIT
From this resource it seems proc-sql uses double pipe || for string contatenation and double quotes for strings so try the new version below.
select * from Table1
join table2
on table1.column like "%" || table2.column || "%"
Try this
select * from Table1
join table2
on table1.column like '%' + table2.column + '%' COLLATE Latin1_General_CS_AS
where (COLLATE Latin1_General_CS_AS) stands for case sensitive in Microsoft SQL Server
Related
I have two tables in database.
Both tables have a business name column but not always going to be the same.
For example tbl 1 has a business name of 'Aone Dental Practices Limited TA Jaws Dental' and Tbl 2 has a business name of 'Jaws Dental'. I want to be able to join these together as Jaws Dental is visible in both.
I can't seem to get the Like clause working for this.
tried
Tbl1_BusinesName Like '%' + Tbl2_BusinesName + '%'
This query should work :
SELECT *
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.BusinesName LIKE '%'+TS.BusinesName+'%'
Using EXISTS you can get the expected result:
SELECT *
FROM dbo.TableName1 AS Tbl1
WHERE EXISTS (SELECT 1
FROM dbo.TableName2 AS Tbl2
WHERE Tbl1.BusinesName LIKE '%' + Tbl2.BusinesName + '%');
I am writing a DB2 Stored procedure where I need to have a condition that select all values from table 1 and table 2 where table1.column_a starts with table2.column_b.
select * from table1 T1, table2 T2 where T1.column_a like T2.column_b + '%'
I tried playing around the above SQL but it seems like an invalid SQL. Any suggestions?
You can use the concat() function or || concatenation operator. + for string concatenation is used by SQL Server and similar databases.
I would phrase this as a join:
select *
from table1 T1 join
table2 T2
on T1.column_a like concat(T2.column_b, '%');
Consider that column_a and column_b has trailing blanks and that there is an issue with like operand. You could always use left() and combine it with RTRIM(). I've have very limited experience with DB2 but this is my hunch.
select *
from table1 T1
INNER JOIN table2 T2
on LEFT(RTRIM(T1.column_a), LENGTH(RTRIM(T2.column_b))) = T2.column_b
I know this question has been asked, but I have a slightly different flavour of it. I have a use case where the only thing I have control over is the WHERE clause of the query, and I have 2 tables.
Using simple example:
Table1 contains 1 column named "FULLNAME" with hundreds of values
Table2 contains 1 column named "PATTERN" with some matching text
so, What I need to do is select all values from Table 1 which match the values in table 2.
Here's a simple example:
Table1 (FULLNAME)
ANTARCTICA
ANGOLA
AUSTRALIA
AFRICA
INDIA
INDONESIA
Table2 (PATTERN)
AN
IN
Effectively what I need is the entries in Table1 which contain the values from Table2 (result would be ANTARCTICA, ANGOLA, INDIA, INDONESIA)
In other words, what I need is something like:
Select * from Table1 where FULLNAME IN LIKE (Select '%' || Pattern || '%' from
Table2)
The tricky thing here is I only have control over the where clause, I can't control the Select clause at all or add joins since I'm using a product which only allows control over the where clause. I can't use stored procedures either.
Is this possible?
I'm using Oracle as the backend DB
Thanks
One possible approach is to use EXISTS in combination with LIKE in the subquery:
select * from table1 t1
where exists (select null
from table2 t2
where t1.fullname like '%' || t2.pattern || '%');
I believe that you can do this with a simple JOIN:
SELECT DISTINCT
fullname
FROM
Table1 T1
INNER JOIN Table2 T2 ON T1.fullname LIKE '%' || T2.pattern || '%'
The DISTINCT is there for those cases where you might have a match to multiple rows in Table2.
If the patterns are always two characters and only have to match the start of the full name, like the examples you showed, you could do:
Select * from Table1 where substr(FULLNAME, 1, 2) IN (Select Pattern from Table2)
Which prevents any index on Table1 being used, and your real case may need to be more flexible...
Or probably even less efficiently, similar to TomH's approach, but with the join inside a subquery:
Select * from Table1 where FULLNAME IN (
Select t1.FULLNAME from Table1 t1
Join Table2 t2 on t1.FULLNAME like '%'||t2.Pattern||'%')
Right, this involved a bit of trickery. Conceptually what I've done is turned the column from the PATTERN into a single cell, and use that with REGEX_LIKE
So the values "AN and IN" becomes one single value '(AN|IN)' - I just feed this to the regexp_like
SELECT FULLNAME from table1 where
regexp_like(FULLNAME,(SELECT '(' || SUBSTR (SYS_CONNECT_BY_PATH (FULLNAME , '|'), 2) || ')' Table2
FROM (SELECT FULLNAME , ROW_NUMBER () OVER (ORDER BY FULLNAME) rn,
COUNT (*) OVER () cnt
FROM Table2)
WHERE rn = cnt START WITH rn = 1 CONNECT BY rn = PRIOR rn + 1))
The subquery in the regexp_like turns the column into a single cell containing the regular expression string.
I do realise this is probably a performance killer though, but thankfully I'm not that fussed about performance at this point
Working in MS SQL 2005 and I want to use a select statement within a wildcard where clause like so:
SELECT text
FROM table_1
WHERE ID LIKE '%SELECT ID FROM table_2%'
I'm looking for product ids within a large body of text that is held in a DB. The SELECT statement in the wildcard clause will return 50+ rows. The statement above is obviously not the way to go. Any suggestions?
You can do a join and construct the like string based on table_2.
SELECT * FROM table_1 t1
INNER JOIN table_2 t2 ON t1.ID LIKE '%' + CONVERT(VARCHAR, t2.ID) + '%'
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 != ''