Solved

String concatenationon field values that exist only

Posted on 2016-09-08
2
26 Views
Last Modified: 2016-10-28
Hello,
I have to update a column within a table where if a certain column(a) has a value.
ideally, if there was a value in all 4 columns, then the concatenation would be '1.1,1.2,1.3,1.4'
if column(a) has a value > 0 then column(x) = '1.1'
if column(b) has a value > 0 then column(x) = '1.1,1.2'
if column(c) has a value > 0 then column(x) = '1.1,1.2,1.3'
if column(d) has a value > 0 then column(x) = '1.1,1.2,1.3,1.4'

like in row 1 below, column b and c do not have any values, so column(x) = '1.1,1.4'
row 2 - '1.1,1.2,1.4'
row 3 - '1.4'

A      B      C      D
---      ---      ---      -----
100      0      0      8000
102      40      0      200
0      0      0      10000


this code will be in a loop that retrieves each of these 4 values in a row.

the code is something like this but this wont work as values like '1.1' might now have a value and not
needed in the concatenation.

--the @exists variables are set to 1 if there is a value > 0...
--@ITEM_TRANS11 = '1.1', @ITEM_TRANS12 = '1.3', etc...  column(a). column(b), etc...
--@ITEM_COMPAT = column(x)
            if @exists1 = 1 begin set @ITEM_COMPAT = @ITEM_TRANS11 end;
            if @exists2 = 1 and @exists1 = 0
                  begin
                        set @ITEM_COMPAT = @ITEM_COMPAT+','+@ITEM_TRANS12
                  end;
            if @exists3 = 1 and @exists1 = 0
                  begin
                        set @ITEM_COMPAT = @ITEM_COMPAT+','+@ITEM_TRANS13
                  end;

I dont want the concatenation to end up like this:
row 1 = '1.1,,,1.4'
row 2 - '1.1,1.2,,1.4'
row 3 - ',,,1.4'


I can use like many conditions to try and check for any combinations but that would be
too much code as a better solumn can be found with limited code created.

using sql server 2008

any help would be appreciated.

thanks,
MG
0
Comment
Question by:Machinegunner
2 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 41790414
This result:
A   B  C D     x           
--- -- - ----- ----------- 
100 0  0 8000  1.1,1.4     
102 40 0 200   1.1,1.2,1.4 
0   0  0 10000 1.4         

Open in new window

from:
declare @table1 table
    ([A] int, [B] int, [C] int, [D] int)
;
    
INSERT INTO @table1
    ([A], [B], [C], [D])
VALUES
    (100, 0, 0, 8000),
    (102, 40, 0, 200),
    (0, 0, 0, 10000)
;

select
        [A]
      , [B]
      , [C]
      , [D]
      , stuff(a1+b1+c1+d1, 1, 1, '') x
from @table1
outer apply (
           select
                case when [a] <> 0 then ',1.1' else '' end  a1
              , case when [b] <> 0 then ',1.2' else '' end  b1
              , case when [c] <> 0 then ',1.3' else '' end  c1
              , case when [d] <> 0 then ',1.4' else '' end  d1
      ) oa

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now