This is what I have:
drop table ttemp
create table ttemp(
col1 char(10),
col2 varchar(100))
INSERT INTO ttemp VALUES('101','shshsfhgs')
INSERT INTO ttemp VALUES('102','ertqehwrgs')
INSERT INTO ttemp VALUES('0102','witpipqcqp')
INSERT INTO ttemp VALUES('0103','retrtyhwqpp')
drop table #temp1
create table #temp1
(ref1 char(10),
refdata varchar(100))
drop table #temp2
Create table #temp2
(ref1 char(10),
refdata varchar(100))
insert into #temp1 values(101,'aaaaaaaaaaaaaa')
insert into #temp1 values(102,'bbbbbbbbbbbbbb')
insert into #temp1 values(103,'cccccccccccccc')
select * from #temp1
insert into #temp2
SELECT t1.col1, #temp1.refdata
FROM ttemp t1
INNER JOIN #temp1 on t1.col1 = #temp1.ref1 OR t1.col1 = '0' + #temp1.ref1
select * from #temp2
What I would LIKE to have, is to get back only 1 row for each value in #temp1.
So if I have 102 in #temp1, and 102 in ttemp I should see that row. (NOT 0102)
If I have 103 in #temp1, but only 0103 in ttemp, then I should see that row (because there is no 103 in ttemp).
So I just want to prioritize the INNER JOIN: if t1.col1 = #temp1.ref1, then just use that one; not the t1.col1 = ‘0’ + #temp1.ref1.
The SELECT from #temp2 shows this now:
ref1 refdata
101 aaaaaaaaaaaaaa
102 bbbbbbbbbbbbbb
0102 bbbbbbbbbbbbbb
0103 cccccccccccccc
And I want this:
ref1 refdata
101 aaaaaaaaaaaaaa
102 bbbbbbbbbbbbbb
0103 cccccccccccccc
If you do it in 2 steps it is quite easy:
Results:
Here’s another way building on Joe Stefanelli’s answer and altering it to produce the results you require:
Results: