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.
Related
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.
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
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) + '%'
Is it possible to have an "IN" statement within a left join clause?
I have two tables with two columns:
Table 1: Table 2:
Column1: Column2:
192192, 192192
119202 119202
810395 810395
975643, 975643
908574,, 908574
As you can see, tbl1.col1 has entries can have entries with commas trailing or have none at all. I need to join the two tables where column 1 contains column 2 or where they are equal. It made sense to be to join where column1 includes column 2. Maybe there's a better way to accomplish the join?
I need to join the two to access data in table 2. So the columns just need to align while allowing for a potential comma or two in column 1. Any suggestions?
If this is just some sort of one-time deal or a staging table and not your real prod table:
(this one works no matter where inteh number the commas are)
select <column list>
from table1 t1
join table2 t2 on replace(t1.col1, ',','') = t2.col2
or
select <column list>
from table1 t1
join table2 t2 on cast(t2.col2 as varchar (10)) like t1.col1+'%'
(you may or may not need the cast depending onthe data types of the tow columns, I was assuming one was varchar and the other was integer)
or
(this only works if the value will always be the same length
select <column list>
from table1 t1
join table2 t2 on left(t1.col1,6) = t2.col2
If this is a real prod table, then you could have performance issues joining on a function, so fix the data instead.
I think rtrim() is perfect for this:
SELECT *
FROM table1 t1
JOIN table2 t2 ON trim(t1.col1, ',') = t2.col2;
Also you may just want to substr function to trim the first table column and use the left outer join as:
using substr:
select t1.col1, t2.col2
from table1 t1 left join table2 t2
on substr(t1.col1, 0, length(t1.col1)-1) = t2.col2;
using replace:
select t1.col1, t2.col2
from table1 t1 left join table2 t2
on replace(t1.col1, ',' , '')= t2.col2;
Both the above queries should work in MySQL and Oracle.
Can anyone give me an example of a sql inner join between 2 tables when using a partial key?
For example:
======
Table1
======
StudentID: STUDENT00001, STUDENT00002, STUDENT00999
======
Table2
======
ID: 00001, 00002, 00999
My code so far:
SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.StudentID = ***how do i add a prefix here?*** Table2.ID
THANKS!
Use the CONCAT() function in MySQL.
SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.StudentID = CONCAT('STUDENT',Table2.ID)
Maybe like this:
SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.StudentID = CONCAT('STUDENT', RIGHT(100000 + Table2.ID, 5))
CONCAT(str1, str2, …) returns a string that is the result of concatenation of str1, str2 etc.
RIGHT(str, len) returns str's rightmost len characters. str in this case is represented by the result of 100000 + Table2.ID, implicitly converted to a string.
I am assuming here that Table2.ID is an integer, so the entire RIGHT() expression is intended to turn the integer into a string where the integer value is padded with zeros. If Table2.ID is a string and is already padded with zeros, you don't need RIGHT, of course. The join condition would then look just like this:
… ON Table1.StudentID = CONCAT('STUDENT', Table2.ID)
Should simply be able to concat
SELECT * FROM Table1 t1 INNER JOIN Table2 t2 ON t1.StudentID = 'STUDENT'+CAST(t2.ID AS NVARCHAR)