Join tables on a partially matched columns - sql

How to join two tables on a partially matched columns in SQL?
For example:
T1.column has the string 'c/ar' and T2.column has the string 'ar' and I want the tables to be joined in this case.
I tried
select column2,
column3
from T1 join T2 on T1.column like '%T2.column';
but it returns 0 rows.

Alternatively, you may use strpos function as
select column1, column2
from T1
join T2 on strpos(T1.column1,T2.column2)>0;
Rextester Demo
The PostgreSQL strpos() function is used to find the position, from where the substring is being matched within the string.
Syntax:
strpos( < string > , < substring >)

You can also use RIGHT() and LENGTH() functions since you are checking if T2.Column exists in the end of T1.Column
ON RIGHT(T1.Column1, LENGTH(T2.Column2)) = T2.Column2
Demo

You need to concatenate '%' with column T2.column and not with the string value 'T2.column':
like ('%' || T2.column)
or
like CONCAT('%', T2.column)

Related

How to use substring function in sql to extract when length of value is unknown

select * from table1 t1 join table2 t2 on
substring('t1.column1',9,x)=t2.column2;
This is the SQL query. i want to do this join on value of column1 after first 9 characters. But the value of column1 is not always consistent in length. What should be the value of 'x' here?
One method would be like:
select *
from table1 t1 join
table2 t2
on t2.column2 like '_________' || t1.column1;
-------------------------^ exactly 9 underscores
Note: This idea should work in any database. However, not all databases support the SQL Standard || string concatenation operator, so you might have to adjust the syntax.

Joining multiple tables by removing leading alpha character

I need to match column.table1 = '1234' to column.table2 = 'B1234'. Is there a way to bypass the leading alpha character in table2? More specifically, can this be achieved by using an INNER JOIN clause?
You can join them via a LIKE.
An underscore _ can match any-1 character in the LIKE syntax.
SELECT *
FROM table1 t1
JOIN table2 t2
ON t2.col LIKE CONCAT('_', t1.col)
In SQL Server, you could do something like this:
SELECT *
FROM table1 t1
INNER JOIN table2 t2 ON CAST(t1.column AS VARCHAR) = SUBSTRING(t2.column,2,LEN(t2.column)-1)
The SUBSTRING() function here takes the value of Table2.column starting at the second position, thus skipping the alpha character at the beginning.

SQL condition - starts with a dynamic value

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

Combine LIKE and IN using only WHERE clause

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

How to get a result set of string using like or compare

Using SQL Server 2008
Table 1 varchar(100)
Values1 (Always one row)
123ABC456
Table2 varchar(200)
Values2 (Mulitple Rows)
123ABC456
123ABC456INV1
123ABC456_JULY
JULY123ABC456
0123ABC456
99123ABC456
JULY 123ABC456 INV 1
123JULYABC456
123_ABC456
I want to select exact value1 match from value2 and First 9 characters values2 should be matched from value1. Below for your reference
123ABC456 - Exact Match
123ABC456INV1 - First 9 Character Matched
123ABC456_JULY - First 9 Character Matched
JULY123ABC456 - No Match
0123ABC456 - No Match
99123ABC456 - No Match
JULY 123ABC456 INV 1 - No Match
123JULYABC456 - No Match
123_ABC456 - No Match
Query like
Select * from table2 where values like '% table1 values %'
Expected Output
123ABC456
123ABC456INV1
123ABC456_JULY
Kindly advise and support for the query
Try this one
SELECT t2.Values2
FROM Table2 t2
INNER JOIN Table1 t1 ON (t2.Values2 LIKE t1.Values1 + '%')
It does not care for Values1's length which is a good thing.
SELECT t2.Values2
FROM Table2 t2
INNER JOIN Table1 t1
ON LEFT(t2.Values2,9) = t1.Values1
Try this.......
select DISTINCT Table2.Value
from Table1
join Table2
on Table1.value = left(Table2.value,9)
You can use LEFT function.
Returns the left part of a character string with the specified number of
characters.
SELECT Table2.Values2
FROM Table2
JOIN Table1
ON LEFT(Table2.Values2, 9) = Table1.Values1
you need to declare the match string first and as you want the same prefix so you should use ='value%'
DECLARE #match NVARCHAR(255)
SELECT #match= (select * from #Table1) + '%';
Select * from #Table2 where value like #match
You can use SUBSTR for this:
SELECT
*
FROM
table1 t1
JOIN table2 t2 ON SUBSTR(t1.values,1,9) = SUBSTR(t2.values,1,9)
Edit: SUBSTR is more database engine agnostic - so, more portable than LEFT.
This is also valid:
SELECT * FROM Table2 WHERE Value2 LIKE (SELECT Value1 FROM Table1) + '%'