Concatenate strings when if string is not empty

Hi,

I have five fields in my table.
I want to create an expression which looks like as below.

Address Line1,Address Line2,City,State,County

Comma should be concatenated when there is a value before and after.

We can use CASE statement but lot of conditions we need to consider.
Is there any other solution.....?

Can any one help me out in this issue.

Thanks in advance.
Anil
nilkumarAsked:
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.

dba9i_barakCommented:
Hi Anil,
hope i understood your Q. correctlly,
if you'r using Oracle DB then:
select nvl(Field1,',')||' '||nvl(field2, ',')||' '||nvl(field3, ',')||' '|| .....
from your_table
where condition.

hope this what you meant

BGH
sujit_kumarCommented:
Address Line1,Address Line2,City,State,County

select decode(nvl(Address_Line1, 'XXX'), 'XXX', '', 'Address_Line1'||',') ||
decode(nvl(Address_Line2, 'XXX'), 'XXX', '', 'Address_Line2'||',') ||
decode(nvl(City, 'XXX'), 'XXX', '', 'City'||',') ||
decode(nvl(State, 'XXX'), 'XXX', '', 'State'||',') || Country
from tab;


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
JesterTooCommented:
If you're using MS SQL Server 2000 you could use a UDF (User Defined Function) like this:

CREATE FUNCTION dbo.MakeCSV(@Field1 as varchar,
                            @Field2 as varchar,
                            @Field3 as varchar,
                            @Field4 as varchar,
                            @Field5 as varchar)
       RETURNS varchar(4000)
AS

BEGIN
Declare @Return as varchar(4000)
Declare @Length as smallint

set @Return = ''
if @Field1 is not null
   set @Return = @Field1
if @Field2 is not null
   set @Return = @Return + "," + @Field2
if @Field3 is not null
   set @Return = @Return + "," + @Field3
if @Field4 is not null
   set @Return = @Return + "," + @Field4
if @Field5 is not null
   set @Return = @Return + "," + @Field5

set @Length = len(@Return)
if @Length > 0
   begin
      --remove any leading comma...
      if left(@Return, 1) = ","
         set @Return = right(@Return, @Length - 1)
   end
Return @Return
END



and this could be your query...

     SELECT dbo.MakeCSV(AddrLine1,Addrline2,City,State,County) AS MyString FROM MyTable
jaspreet_bajajCommented:
I think procedure/function is best way to do that even if you are using Oracle

But I tried this, You need to expand this
NVL2(Country, NVL2(State, State || ',' || country, Country), State)

Syntax:
NVL2(expr_1, expr_2, expr_3)
Returns expr_3 if expr_1 Is NULL
       else expr_2 (expr_1 Is Not NULL).

Example:
SQL> select nvl2(1,2,3) from dual;
NVL2(1,2,3)
-----------
          2

Question : Out of all these fields, only one can be empty or its random?
I mean is there a fixed pattern or not

Jaspreet
HilaireCommented:
I think this is not worth the overhead of a user-defined function call
What's your DBMS ?
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
Databases

From novice to tech pro — start learning today.