Solved

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

Posted on 2008-10-10
6
215 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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

776 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