Solved

String concatenationon field values that exist only

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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…

705 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

18 Experts available now in Live!

Get 1:1 Help Now