SQL: Edit data before join - sql

ORACLE: I am trying to LEFT OUTER JOIN a table, on PART column.
First Part Column is formatted as following: "XXXXXXXX".
Column number two is formatted as following: "ABCXXXXXXXX".
I want to exclude "ABC" before "XXXXXXXX". is there a formula for doing this?
Thank you in advance.

You could join using the substring on the second column starting with the fourth character, e.g.
SELECT t1.*, t2.*
FROM table1 t1
LEFT JOIN table2 t2
ON t1.PART = SUBSTR(t2.PART, 4);

Try using LIKE, is an operator used to search for a specified pattern in a column.
Try something like this:
column2 LIKE '%' + column1
for mysql you need use concat('%', column1)
It finds any values that end with column1 value

I would recommend:
SELECT t1.*, t2.*
FROM table1 t1 LEFT JOIN
table2 t2
ON t2.PART = 'ABC' || t1.PART ;
This allows Oracle to use an index on t2.PART. For an INNER JOIN this would not matter. But in a LEFT JOIN, table1 is going to be scanned anyway. This may allow Oracle to use the an index on table2(PART).
Or more generally:
SELECT t1.*, t2.*
FROM table1 t1 LEFT JOIN
table2 t2
ON t2.PART LIKE '___' || t1.PART ;

Related

How to use Substring in left outer join

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

sql join with like operator

Hi I have 2 tables in my sqltable in which my first table has id's like 111,112,113 etc. and second table has same column also have column id which contains values like 111|aa,112|ab,114|ad and i need to select all the ids from second table which contain part of id of first column like 2nd table contains 111 and 112 which also there in table1 as 111 and 112 how can i select those recods itried below query but didbt get result.
select t1.id,t2.id from table1 t1 left outer join table2 t2 where t2.id like t2.id +'%'
also tried.
select t1.id,t2.id from table1 t1 left outer join table2 t2 on t2.id like t2.id +'%'
can someone please give me a hint how should i do this.
You have to use CONCAT to add the % character:
SELECT t1.id,t2.id
FROM table1 t1 LEFT JOIN table2 t2 ON t2.id LIKE CONCAT(t1.id, '|%')
demo on dbfiddle.uk
Note: Also make sure you are using the correct columns on the ON clause. In the above demo and query t1.id is the numeric column (contains 111, 112 or 113) and t2.id is the string column (contains 111|aa, 112|ab or 114|ad).
To avoid unexpected behaviour on numbers greater than 999 you can add the pipe character | to the condition too.
You could try usingb a proper ON clause
select t1.id,t2.id
from table1 t1
left outer join table2 t2 ON t1.id like concat(t2.id, '%')
or
select t1.id,t2.id
from table1 t1
left outer join table2 t2 ON t2.id like concat(t1.id, '%')
for the join you should use the column from two table not from the same table

Oracle Query comparing two table

I have two tables T1 and T2.
T1 having records like A,B,C,D
T2 having records like A,B,D,E
Now out of the query should be C when we compare both tables as C is not available in T2
Please help here..
In Oracle, you can use the minus set operator:
select t1.*
from t1
minus
select t2.*
from t2;
You should be able to just use an inner join, this will return everything that both tables have in common, so:
SELECT T1.* FROM T1 INNER JOIN T2 ON T1.id = T2.id
Another way of achieving this would be:
SELECT
C1
FROM
T1
WHERE
C1 NOT IN (
SELECT
C1
FROM
T2
);

select value from another table if record is found else, use the value on current table

i'm trying to get the values from another table2 if a match exist, else, select the value in table1 but it's taking a long time to execute the query.
select table1.field1, table1.field2,
case
when exist (select top 1
from table2
where table2.field1=table1.field3
and table2.field2 is not null
order by date desc)
then (select top 1
from table2
where table2.field1=table1.field3
and table2.field2 is not null
order by date desc)
else table1.field3
end
from table1
any other way to re-write this query?
please help! n00b here :(
Yes. Use outer apply:
select t1.field1, t1.field2, coalesce(t2.??, t1.field3)
from table1 t1 outer apply
(select top 1 t2.*
from table2 t2
where t2.field1= t1.field3 and t2.field2 is not null
order by t2.date desc
) t2;
It is unclear what field you are talking about, because it is missing from the question; hence, the ??.
SELECT t1.field1, t1.field2, coalesce(t2.field2,t1.field3) as field3
FROM table1 t1
LEFT JOIN table2 t2
on t2.field1=t1.field3
and t2.date = (select max(date) from table2 t2e where t2.field1=t2e.field2)
SELECT
table1.field1,
table1.field2,
COALESCE(table2.field2,table1.field3)
FROM
table1
LEFT OUTER JOIN
table2
ON
table2.field1=table1.field3
COALESCE skips all the null values in the list.
LEFT OUTER JOIN returns all values from the first table, but puts null values where there is no corresponding row in the second table.

SQL: expading "in (...)" to "like"

I have a sql select like below:
select * from table1 where text in (select text from table2)
In real in in clausule is more complex select that this. text is string (varchar). How to expand that sql to select rows from table1 where text is like texts from table2 (not only exactly equals) ?
If you have your wild card expressions in the text column in Table2 you can do like this.
select *
from Table1 as T1
where exists (select *
from Table2 as T2
where T1.[text] like T2.[text])
Otherwise you need to add the % in your query.
select *
from Table1 as T1
where exists (select *
from Table2 as T2
where T1.[text] like '%'+T2.[text]+'%')
Something like this:
select *
from table1
where exists
(
select *
from table2
where table1.text like N'%'+table2.text+N'%'
)
Note
This may be a performance killer
I know I am late to the party here, but you can also just do this with a JOIN if you need the wildcard text as part of the query (as I did). Assuming you have all the entries to check against in table2.text,
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.text LIKE '%' + t2.text + '%'