SQL Server Forums
Profile | Register | 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)
 adding in sql using a value from a new column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

s1lentb0b
Starting Member

3 Posts

Posted - 11/12/2012 :  07:59:55  Show Profile  Reply with Quote
Hi

First time poster.

I have a set of data that is a list of names and a list of values next to them. There is a 3rd column that is for an amended value. Basically I want to add the values together but if there is a value in the 3rd column I want to use that one instead of the real value.

hope I have explained this well.

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/12/2012 :  08:16:11  Show Profile  Reply with Quote
One of these two:
SELECT
	[name],
	COALESCE(thirdColumn,SecondColumn) AS [Values]
FROM
	YourTable;
	
	
SELECT
	SUM(COALESCE(thirdColumn,SecondColumn)) AS SumOfValues
FROM
	YourTable;
Go to Top of Page

s1lentb0b
Starting Member

3 Posts

Posted - 11/12/2012 :  09:11:12  Show Profile  Reply with Quote
will that ignore the first value and just use the amended value when there is one?
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/12/2012 :  10:40:47  Show Profile  Reply with Quote
Yes - if the third column is NULL. COALESCE function goes down the argument list, starting at the first one. If the first one is not null, it picks that value. If that is null, it picks the second value, and so on.

http://msdn.microsoft.com/en-us/library/ms190349.aspx
Go to Top of Page

s1lentb0b
Starting Member

3 Posts

Posted - 11/14/2012 :  05:39:01  Show Profile  Reply with Quote
excellent thanks for the help.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.06 seconds. Powered By: Snitz Forums 2000