I am new to SQL, so please bear with me.
I am working with two tables that do not have common fields. I am trying to create simple hierarchy based on the two tables that look like this:
Table 1.
Cul 1 Cul 2
==============
S10000 Name
S20000 Name 1
S30000 Name 2
S40000 Name 3
Table 2
Cul 1 Cul 2
=====================
A10000 Test
A10001 Test 123
A20000 Test 1
A20001 Test 999
A30000 Test 2
A30002 Test 5555
A40000 Test 3
A40006 Test 84384848
I would like to write a query that will display 'Name' field from Table 1 based on matching numeric values in the first columns from table 1 & 2.
So if Table 1 is S10000, display A1000 – Test
Is that possible?
Thank you
Your requirements are not exactly clear but it sounds like you want this:
select t1.col1 t1Name,
t1.col2 t1Value,
t2.col1 t2Name,
t2.col2 t2Value
from table1 t1
inner join table2 t2
on substring(t1.col1, 2, 1) = substring(t2.col1, 2, 1)
edit, you can also use:
select t1.col1 t1Name,
t1.col2 t1Value,
t2.col1 t2Name,
t2.col2 t2Value
from table1 t1
inner join table2 t2
on substring(t1.col1, 2, len(t1.col1))
= substring(t2.col1, 2, len(t2.col1));
SELECT
t2.*
FROM Table1 t1
INNER JOIN Table2 t2 on SUBSTRING(t1.Cul1,2,LEN(t1.Cul1)-1)=SUBSTRING(t2.Cul1,2,LEN(t2.Cul1)-1)
If you are very sure that the first digit will be the digit and followed by the numeric then the below should work
SELECT A.cul1,B.cul2
FROM dbo.Table1 A, dbo.Table2 B
WHERE substring(A.cul1,2,len(A.cul1))= substring(B.cul1,2,len(B.cul1))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.