How to use Substring in left outer join - sql

I tried to join these 2 tables with ALTERNATE_ID's using substring in left outer join but it doesn't work. Can someone suggest me how to do it.
Here Table1 is master table,
SELECT
tab1.USER_NAME,
tab2.ALTERNATE_ID as 'Contract_no'
FROM TABLE1 tab1
LEFT OUTER JOIN TABLE2 tab2 ON Table2.ALTERNATE_ID = SUBSTRING(tab1.ALTERNATE_ID,0,CHARINDEX('/',tab1.ALTERNATE_ID)
TABLE1:
ALTERNATE_ID 100-0000053-001/0001
TABLE2
ALTERNATE_ID 100-0000053-001

You have to use substring from first char to position of \ - 1
But if you have to join based on part of column, then you should created index on the substring part.
SELECT
tab1.USER_NAME,
tab2.ALTERNATE_ID as 'Contract_no'
FROM TABLE1 tab1
LEFT OUTER JOIN TABLE2 tab2
ON Table2.ALTERNATE_ID
=
SUBSTRING(tab1.ALTERNATE_ID,1,CHARINDEX('/',tab1.ALTERNATE_ID-1)

You can also use SUBSTRING_INDEX to split the string like this:
SELECT tab1.USER_NAME, tab2.ALTERNATE_ID as 'Contract_no'
FROM TABLE1 tab1
LEFT OUTER JOIN TABLE2 tab2
ON Table2.ALTERNATE_ID = SUBSTRING_INDEX(tab1.ALTERNATE_ID, "/", 1);

You can use OUTER APPLY in the following way:
SELECT
tab1.USER_NAME,
tab2.ALTERNATE_ID as 'Contract_no'
FROM TABLE1 tab1
OUTER APPLY (Select ALTERNATE_ID
From TABLE2
Where ALTERNATE_ID=SUBSTRING(tab1.ALTERNATE_ID,1,CHARINDEX('/',tab1.ALTERNATE_ID)-1)) As tab2

If you use PATINDEX, then it's easy to get the position of the digit before the slash.
SELECT *
FROM TABLE1 t1
LEFT JOIN TABLE2 t2
ON t2.ALTERNATE_ID = LEFT(t1.ALTERNATE_ID, PATINDEX('%[0-9]/%', t1.ALTERNATE_ID))
Or looking at it differently, the one in Table1 is like the one in Table2
SELECT *
FROM TABLE1 t1
LEFT JOIN TABLE2 t2
ON t1.ALTERNATE_ID LIKE t2.ALTERNATE_ID+'/%'
Demo on db<>fiddle here

Related

Inner join the tables where the value of one column is different. But the meaning of the values are same

Table t1
Table t2
I tried running the query
select CountyCode, ContactPerson
from table1 t1
inner join select * from table 2 t2 on t1.CountyCode[1] = Code[0]
Any one help me please.
Use this syntax:
select CountyCode, ContactPerson
from t1
inner join t2 on t1.CountyCode = t2.Code

How to Select the Joins?Right or Left

How to decide the tables which is left and which is right while querying it using joins?Is there any Thumb Rule.
"left" is that table which appears first after the FROM clause when reading from left to right
example:
select t1.* ,t2.* from table1 t1 left join table2 t2 on t1.id=t2.id
here table1 is left table

create a sql join with more columns

i want create a sql statemnt (in PL SQL Developer) with a join with comma seperated?
SELECT * FROM TABLE1 t1 JOIN TABLE2 t2 ON t1.tab_id, second_id = t2.tab_id, second_id;
I always get a ORA-00920 Exception. If i change it to two Rows:
t1.tab_id = t2.tab_id AND t1.second_id = t2.second_id;
Then i get rows.
Can some say me if i can use the first step with coma seperated columns?
Greetz
You need a valid condition:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
ON t1.tab_id = t2.tab_id AND t1.second_id = t2.second_id;
I think Oracle will also let you do:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
ON (t1.tab_id, t1.second_id) in ( (t2.tab_id, t2.second_id) );
Or even:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
USING (tab_id, second_id);
This works because the JOIN keys have the same names in the two tables.

SQL join order and conditions

I've got 3 tables that I want to join and filter on some conditions.
I've first wrote this query:
select * from table1 t1
left join (select * from table2 where table2.fieldX=...) t2
on t1.id_12=t2.id_12
left join table3
on t2.id_23=t3.id_23
where t1.fieldY=...
Then I wanted to make it looks like more canonical by rewritting it like that:
select * from table1 t1
left join table2 t2
on t1.id_12=t2.id_12
left join table3
on t2.id_23=t3.id_23
where table2.fieldX=...
and t1.fieldY=...
But it does not give the same result.
I dont't understand why...
Do you?
Thanks in advance.
When you put table2.fieldX=... in the where clause you eliminate all rows from table1 that do not have a corresponding row in table2. Effectively you are changing the left join into an inner join.
Instead, you can apply the table2 filter in the join itself:
SELECT
*
FROM
Table1 t1
LEFT JOIN Table2 t2 ON t1.id_12 = t2.id_12 AND t2.fieldX = ...
LEFT JOIN Table3 t3 ON t2.id_23 = t3.id_23
WHERE
t1.fieldY = ...
Did you add the inner select where on the second query?
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
on t1.id_12=t2.id_12
LEFT JOIN table3
on t2.id_23=t3.id_23
WHERE t1.fieldY=...
and t2.fieldX=...

How to make a proper cross join

I want to cross join the two table
Query
Select * from table1 cross join select * from table2
Above query is showing error.
What wrong in my query
Reference
The result set of the Cross Join is the number of rows in the first table multiplied by the number of rows in the second table
SELECT T1.Columns1, T1.Column2, T2.ColumnName FROM Table1 T1
CROSS JOIN Table2 t2
You must use select like this : SELECT T1.FIELD1, T2.FIELD2 FROM TABLE1 AS T1 CROSS JOIN TABLE2 AS T2 - FIELD[X] FOR EXAMPLE ONLY
Try this:
SELECT * FROM table1, table2
or this:
select * from table1 CROSS JOIN table2;