Solved

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

Posted on 2008-10-10
6
212 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now