Solved

String concatenationon field values that exist only

Posted on 2016-09-08
2
29 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

820 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