Solved

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

Posted on 2008-10-10
6
209 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
Comment Utility
here's a known example that matches:

MSFC4570 - from #temp2
MSFC4570 - from #temp3

Thx
0
 
LVL 10

Expert Comment

by:dwe761
Comment Utility
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
Comment Utility
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 10

Accepted Solution

by:
dwe761 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

771 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

12 Experts available now in Live!

Get 1:1 Help Now