Solved

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

Posted on 2008-10-10
6
217 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

830 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