Solved

known mainid matches are not displaying in result set - MS SQL 2005

Posted on 2008-10-10
6
223 Views
Last Modified: 2012-06-27
I have two tables that I had to generate IDs on.  I have reiewed the result sets from each table and see matches but when I run a query to list the matches, I'm getting 0 results...any ideas?

Here's the code.....thx

drop table #temp2
select center, bld, (ltrim(rtrim (Center))+''+ltrim(rtrim (Bld))) as mainid
into #temp2
from dbo.temp1

drop table #temp3
select lu.center_desc as Center, f.building_nos, b.Building,
(ltrim(rtrim (lu.center_desc))+''+ ltrim(rtrim(b.building))) as mainid
into #temp3
from tbl_facility f join LU_Center_Site lu on f.centerid = lu.centerid
join dbo.tbl_Buildings b on b.facilityid = f.facilityid

delete from #temp2 where mainid is null
delete from #temp3 where mainid is null

select mainid from #temp2 where mainid in (select mainid from #temp3)
0
Comment
Question by:Glen_D
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 

Author Comment

by:Glen_D
ID: 22685334
here's a known example that matches:

MSFC4570 - from #temp2
MSFC4570 - from #temp3

Thx
0
 
LVL 10

Expert Comment

by:dwe761
ID: 22686269
As a test, try just selecting the ID directly from the table:
Select * FROm #temp2 where mainid = 'MSFC4570'
Select * FROm #temp3 where mainid = 'MSFC4570'
 
0
 

Author Comment

by:Glen_D
ID: 22686519
That works but I figured out a work around...I concatenated the fields in Excel and then reimported the data...worked after that.  I don't understand if I trim all spaces in MS SQL, the formats for each field were the same, why I couldn't pull the matches.  No NULLS were in either table as well....

Thx for your help though
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 10

Accepted Solution

by:
dwe761 earned 500 total points
ID: 22686532
Also, try using regular tables rather than temp tables and after your code runs, generate the script on the two tables to see if they are coming out with the exact same data types on the fields being joined.
I tried your script on a single table where the inputs are the same and it works.  That leads me to believe that after you have concatinated different fields from different tables having possibly different data types, that although the new fields look similar, they may not be the same to SQL Server.
0
 
LVL 10

Expert Comment

by:dwe761
ID: 22686601
You could also force the data type on the numeric part of the ID before concatinating it because that is probably what Excel implicitly did for you.  See below.
 
 

drop table #temp2
select center, bld, (ltrim(rtrim (Center))+''+ltrim(rtrim (CONVERT(nvarchar(10),Bld)))) as mainid
into #temp2
from dbo.temp1
 
drop table #temp3
select lu.center_desc as Center, f.building_nos, b.Building,
(ltrim(rtrim (lu.center_desc))+''+ ltrim(rtrim(CONVERT(nvarchar(10),b.building)))) as mainid
into #temp3
from tbl_facility f join LU_Center_Site lu on f.centerid = lu.centerid
join dbo.tbl_Buildings b on b.facilityid = f.facilityid
 
delete from #temp2 where mainid is null
delete from #temp3 where mainid is null
 
select mainid from #temp2 where mainid in (select mainid from #temp3) 

Open in new window

0
 

Author Comment

by:Glen_D
ID: 22686651
Thx...I tried convert and cast and just to be sure, I ran your query and I had the same 0 result...I'll keep the ideas for future reference though...G
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question