data extraction !!

Hi.

I am extracting some data from the database.
Theses are four fields i.e. 1,2,3,4 which should have a value+unit as shown below.the fifth field i,e. 5 carries just the unit.

I want that if the fields are blank it should not report anything. In this case it reports a unit.
And if the field has a value it should report the value as well as the unit.

How to do it?

1.  value field||'~'||unit field ||','||
 2.   value field  ||'~'||unit field ||','||
   3. value field  ||'~'||unit field ||','||
   4. value field ||'~'||unit field ||','||
   5. unit field||','||
AmmarDJAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ArgentiCommented:
select

valuefield1 + '~' + unitfield1 + ',' +
valuefield2 + '~' + unitfield2 + ',' +
valuefield3 + '~' + unitfield3 + ',' +
valuefield4 + '~' + unitfield4 + ',' +
valuefield5 + ',' as OneBigField

from yourtable

Open in new window

0
dsackerContract ERP Admin/ConsultantCommented:
You are using the Oracle concatenation "||" rather than the MS SQL one "+".

Are you sure this post belongs in MS SQL Server?
0
ArgentiCommented:
Sorry, I forgot to test the null values. Here:

select

iif(valuefield1 is null, unitfield1, valuefield1 + '~' + unitfield1) + ',' +
iif(valuefield2 is null, unitfield2, valuefield2 + '~' + unitfield2) + ',' +
iif(valuefield3 is null, unitfield3, valuefield3 + '~' + unitfield3) + ',' +
iif(valuefield4 is null, unitfield4, valuefield4 + '~' + unitfield4) + ',' +
unitfield5 + ',' as OneBigField

from yourtable

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.