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)
 Select three fields as 1 field
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

macca
Posting Yak Master

Ireland
146 Posts

Posted - 10/03/2012 :  11:04:29  Show Profile  Reply with Quote
I have 3 fields in a table called Paid1, Paid2 and Paid3.

I have a single column in my report called Paid.

I want that when I run my report that the value contained in Paid1 or Paid2 or Paid3 will display under Paid on my report.
Unfortunately my records dont have the paid value in the same field in the DB.

Anyone know how to do this?

Thanks.

Mike Jackson
Starting Member

37 Posts

Posted - 10/03/2012 :  11:18:08  Show Profile  Reply with Quote
You don't mention which field the Paid is linked to.

If you want to display one of the Paid1 or Paid2 or Paid3. You could use

select COALESCE(Paid1, Paid2, Paid3) as paid
from yourfile

That will give you the first non-null value.

But is there is something in each field that needs to be added together?

Mike
Go to Top of Page

macca
Posting Yak Master

Ireland
146 Posts

Posted - 10/03/2012 :  11:38:26  Show Profile  Reply with Quote
What I mean is that in some records Paid1 contains data, in some Paid1 and Paid2 contains data and in others all three contain data.

What I want is to display a record for each instance of each record that contains a value in either of the three paid fields. And for each record the paid field value is displayed in the paid column of the record ...
Go to Top of Page

lazerath
Constraint Violating Yak Guru

USA
343 Posts

Posted - 10/04/2012 :  16:42:05  Show Profile  Reply with Quote
I have a little trouble understanding you. It would help if you had DDL, sample data and expected output. In the absence of that, see if this pattern works for you:

SELECT [other columns], Paid1 FROM dbo.MyTable WHERE Paid1 IS NOT NULL
UNION ALL
SELECT  [other columns], Paid2 FROM dbo.MyTable WHERE Paid2 IS NOT NULL
UNION ALL
SELECT  [other columns], Paid3 FROM dbo.MyTable WHERE Paid3 IS NOT NULL



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.09 seconds. Powered By: Snitz Forums 2000