admin管理员组文章数量:1401609
I have two different tables with a column called name. I will refer to the tables as Names1 and Names1a. One row in Names1, for example has data formatted as XXX10.R.999.15, Names1a has all of the characters and dots dropped, and is 199915 with a character at the end. The R shown in Names1 could be R or A, but to Names1A, if the value is R, Names1A has an A at the end of it, and is 1099915A. If Names1 has an C, then names1A would have a D at the end making it 1099915D. Ultimately, I'd need Names1 value to reflect the same as Names1A value to join the two tables, which are in separate databases.
I used substrings to extract the unnecessary characters from Names1 and tried case expression for the determination of the end value, but when I try to join the results an getting an error that the values aren't columns. Any ideas on how I can make this work?
Table1:Names1
Name. |
---|
XX10.R.999.15 |
XX10.C.999.15 |
I have two different tables with a column called name. I will refer to the tables as Names1 and Names1a. One row in Names1, for example has data formatted as XXX10.R.999.15, Names1a has all of the characters and dots dropped, and is 199915 with a character at the end. The R shown in Names1 could be R or A, but to Names1A, if the value is R, Names1A has an A at the end of it, and is 1099915A. If Names1 has an C, then names1A would have a D at the end making it 1099915D. Ultimately, I'd need Names1 value to reflect the same as Names1A value to join the two tables, which are in separate databases.
I used substrings to extract the unnecessary characters from Names1 and tried case expression for the determination of the end value, but when I try to join the results an getting an error that the values aren't columns. Any ideas on how I can make this work?
Table1:Names1
Name. |
---|
XX10.R.999.15 |
XX10.C.999.15 |
Table2:Names1A
Name. |
---|
1099915A |
1099915D |
Results would be a new column to Names1 called new names: Table: Names1
Name. | New_names |
---|---|
XXX10.R.999.15. | 1099915A |
XXX10.C.999.15. | 1099915D |
Code:
SELECT
SUBSTRING(Name,7,1) + SUBSTRING(name,9,3) + SUBSTRING(name,13,2) AS new_names
, CASE
WHEN SUBSTRING(name,7,1) LIKE 'R' THEN ('A')
WHEN SUBSTRING(name,7,1) LIKE 'A' THEN ('D')
END AS directions
, CONCAT(new_names,direction)
FROM [case1].[case].[example]
INNER JOIN [names1A].[names] ON [names1].[new_names]
Share
Improve this question
edited Mar 23 at 20:57
Dale K
27.5k15 gold badges58 silver badges83 bronze badges
asked Mar 23 at 2:37
Inquiring MindInquiring Mind
193 bronze badges
8
|
Show 3 more comments
3 Answers
Reset to default 0You can use APPLY to 'create' data values 'in order', and reference them in subsequent steps / joins, etc.
CREATE TABLE example (name VARCHAR(32));
CREATE TABLE name (name VARCHAR(32));
INSERT INTO
example
VALUES
('XXX10.R.999.15'),
('XXX10.C.999.15'),
('XXX10.Z.999.15')
;
INSERT INTO
name
VALUES
('1099915A'),
('1099915D'),
('1099915X')
;
6 rows affected
SELECT
*
FROM
[example] AS e
CROSS APPLY
(
SELECT
SUBSTRING(e.name,7,1)
)
AS old_code(value)
CROSS APPLY
(
SELECT
CASE old_code.value
WHEN 'R' THEN 'A'
WHEN 'C' THEN 'D'
ELSE old_code.value
END
)
AS new_code(value)
CROSS APPLY
(
SELECT
CONCAT(
SUBSTRING(e.name, 4, 2),
SUBSTRING(e.name, 9, 3),
SUBSTRING(e.name, 13, 2),
new_code.value
)
)
AS new_name(value)
FULL OUTER JOIN
[name] AS n
ON n.name = new_name.value
name | value | value | value | name |
---|---|---|---|---|
XXX10.R.999.15 | R | A | 1099915A | 1099915A |
XXX10.C.999.15 | C | D | 1099915D | 1099915D |
XXX10.Z.999.15 | Z | Z | 1099915Z | null |
null | null | null | null | 1099915X |
fiddle
Join tables on condition
select t1.Name,t2.Name New_names
from Names1 t1
left join Names1A t2 on
t2.Name=concat(substring(t1.name,3,2)
,substring(t1.name,8,3),substring(t1.name,12,2)
,case substring(t1.name,6,1)
when 'R' then 'A'
when 'C' then 'D'
else 'X' end
)
Name | New_names |
---|---|
XX10.R.999.15 | 1099915A |
XX10.C.999.15 | 1099915D |
fiddle
Do a translate and replace to eliminate the unnecessary characters. Tack on the final suffix by checking the character in the 6th position.
/* join condition */
on replace(translate(Names1, 'XACDR.', '______'), '_', '') + case substring(Names1, 6, 1) when 'R' then 'A' when 'C' then 'D' else '' end = Names1A
本文标签: sqlMerging substrings from two tables to a joinStack Overflow
版权声明:本文标题:sql - Merging substrings from two tables to a join - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744298773a2599475.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
select * from table1 t1 join table2 T2 on substring_etc(something_or_other_from_table2) = substring_etc(something_or_other_from_table1)
– siggemannen Commented Mar 24 at 7:42