Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Filling holes in query data
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

reidkell
Starting Member

USA
16 Posts

Posted - 09/23/2013 :  17:10:28  Show Profile  Reply with Quote
Hi, all. This is probably very simple, but is escaping me. Here is a greatly simplified version of a query of mine:

SELECT HostName, IPAddress, SerialNum, Mfg FROM tblMaster

Out of 5K rows, there are 100 with missing serial numbers. I have the missing serial numbers in another table (obtained by another feed), but don't know how to write a single TSQL statement to fill those 100 missing serial numbers. Any help appreciated.

tkizer
Almighty SQL Goddess

USA
38200 Posts

Posted - 09/23/2013 :  17:11:59  Show Profile  Visit tkizer's Homepage  Reply with Quote
update m
set SerialNum = t.SerialNum
from tblMaster m
join SomeOtherTable t
on m.HostName = t.HostName --or whatever your linking column is
where m.SerialNum is null

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

reidkell
Starting Member

USA
16 Posts

Posted - 09/25/2013 :  20:41:42  Show Profile  Reply with Quote
Thank you. What's escaping me is a means of accomplishing this from a SELECT statement, sans an UPDATE. Is this possible?
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 09/26/2013 :  02:27:48  Show Profile  Reply with Quote
SELECT HostName, IPAddress, SerialNum, Mfg FROM tblMaster
UNION ALL
SELECT HostName, IPAddress, SerialNum, Mfg FROM OtherTable
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

Posted - 09/26/2013 :  02:53:58  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Using Tara's example.
SELECT		m.HostName, 
		m.IPAddress, 
		COALESCE(m.SerialNum, t.SerialNum) AS SerialNum,
		m.Mfg
FROM		dbo.tblMaster AS m
LEFT JOIN	dbo.SomeOtherTable AS t ON t.HostName = m.HostName	--or whatever your linking column is



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

reidkell
Starting Member

USA
16 Posts

Posted - 09/26/2013 :  12:21:41  Show Profile  Reply with Quote
Yes!! COALESCE did it. I had not used that before. Many thanks!!
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000