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

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Select three fields as 1 field

Author  Topic 

macca
Posting Yak Master

146 Posts

Posted - 2012-10-03 : 11:04:29
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 - 2012-10-03 : 11:18:08
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

146 Posts

Posted - 2012-10-03 : 11:38:26
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

343 Posts

Posted - 2012-10-04 : 16:42:05
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
   

- Advertisement -