Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6958635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:09:57+00:00 2026-05-27T15:09:57+00:00

This is what I have: drop table ttemp create table ttemp( col1 char(10), col2

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:09:57+00:00Added an answer on May 27, 2026 at 3:09 pm

    If you do it in 2 steps it is quite easy:

    1. Insert high priority rows into the output table first
    2. Insert low priority rows into the output table excluding any that are already
      present from the first insert. (Achieved with a left join in this case)
    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') 
    
    create table #temp1 
    (ref1 char(10), 
     refdata varchar(100)) 
    
        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') 
    
    INSERT INTO #temp2
    SELECT t1.col1, t.refdata
    FROM ttemp t1
    INNER JOIN #temp1 t on t1.col1 = t.ref1
    
    
    INSERT INTO #temp2
    SELECT t1.col1, t.refdata
    FROM ttemp t1
    INNER JOIN #temp1 t on t1.col1 = '0' + t.ref1
    LEFT JOIN #temp2 t2 ON t.ref1 = t2.ref1
    WHERE ISNULL(t2.ref1,0) = 0
    
    SELECT * FROM #temp2
    
    drop table ttemp 
    drop table #temp1 
    drop table #temp2 
    

    Results:

    ref1    refdata 
    101     aaaaaaaaaaaaaa 
    102     bbbbbbbbbbbbbb 
    0103    cccccccccccccc
    

    Here’s another way building on Joe Stefanelli’s answer and altering it to produce the results you require:

    SELECT t1.col1, COALESCE(ta.refdata,t.refdata ) [refdata]
    FROM ttemp t1 
    LEFT JOIN #temp1 t ON t1.col1 = t.ref1 
    LEFT JOIN #temp1 ta ON t1.col1 = '0' + ta.ref1 AND ta.ref1 NOT IN (SELECT ref1 FROM ttemp INNER JOIN #temp1 ON col1 = ref1 )
    WHERE ta.ref1 IS NOT NULL
    OR t.ref1 IS NOT NULL
    

    Results:

    col1    refdata
    101     aaaaaaaaaaaaaa
    102     bbbbbbbbbbbbbb
    0103    cccccccccccccc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a testdata like this: DROP TABLE SELECT_PASS; CREATE TABLE SELECT_PASS(ID INT(20),TESTCASE VARCHAR(20),RESULT
I have this table CREATE TABLE VACCINE ( CVX INTEGER, CPT CHAR(5), SHORTNAME VARCHAR(20),
I have created a table like this: CREATE TABLE #TEMP(RecordDate datetime, First VARCHAR(255), Last
I have the following Query: create table #Result (Reward varchar(40), Value MONEY); insert #Result
I have this sql: ALTER TABLE dbo.ChannelPlayerSkins DROP CONSTRAINT FK_ChannelPlayerSkins_Channels but apparently, on some
I have a script which has a DROP TABLE command like this: IF EXISTS
I have a drop down like this on my page: <p> <%= f.label :episode_id
I have n drop-downs like this: <select id=select1> <option>1</option> <option>2</option> <option>3</option> </select> <select id=select2>
This would be my issue I have a drop down that's not displaying fully.
I'm soon to launch a beta app and this have the option to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.