0

I am wondering if there is a straightforward way of concatenating a string based on the value of a field directly in SQL. I know it would be preferable to do in an application, but in this instance, I am only able to use SQL. For example, the following table:

Labels | Qty | LabelQty | OutputString
-------+-----+----------+--------------
     1 |  30 |       30 | NULL
     2 |  60 |       30 | NULL
     2 | 120 |       60 | NULL

I would like to end up with the OutputString like so:

Labels | Qty | LabelQty | OutputString
-------+-----+----------+--------------
     1 |  30 |       30 | 30|
     2 |  60 |       30 | 30|30|
     2 | 120 |       60 | 60|60|

I know this is very easy to do in C# or VB, but I am having a hard time thinking about how to accomplish this in straight SQL. Would I need to use a cursor and do each row one at a time? Unfortunately, I can't just use the string multiplier like in Ruby, such as:

SELECT (CONVERT(VARCHAR(10), LabelQty) + '|') * Labels

Any pointers are much appreciated.

7
  • 1
    what is the logic behind the output of the outputstring? Commented Feb 7, 2014 at 20:24
  • 1
    You have to tell us how you arrive at the contents of output string? Do you want the LabelQty field repeated the number of times as Labels contains with | between? Commented Feb 7, 2014 at 20:24
  • Repeating the LabelQty number of Labels times? Commented Feb 7, 2014 at 20:26
  • Correct, as the bottom SELECT indicates. Repeat the LabelQty field Labels number of times. Commented Feb 7, 2014 at 20:29
  • 1
    dub, the select at the bottom DOES NOT indicate that since * is not a valid operator for a string and this would result in a syntax error. Better to use English for this requirement. Commented Feb 7, 2014 at 20:30

3 Answers 3

6

You can use REPLICATE() function:

select replicate(cast(LabelQty as varchar(100)) + '|', Labels)
from mytable_1
Sign up to request clarification or add additional context in comments.

3 Comments

Wow, where has REPLICATE() been all my life! This is exactly what I was looking for, thanks!
@dubstylee - REPLICATE() has been dating your twin brother.
1+ for too fast answering. I withdraw my answer.
0

You can try

SELECT REPLICATE(CONVERT(VARCHAR(10), LabelQty ) + '|',Labels )

Comments

-2

While you haven't described how to produce output string, but I think that this can be done with case expression:

http://technet.microsoft.com/en-us/library/ms181765.aspx

And sample from that article:

SELECT   ProductNumber, Name, "Price Range" = 
      CASE 
         WHEN ListPrice =  0 THEN 'Mfg item - not for resale'
         WHEN ListPrice < 50 THEN 'Under $50'
         WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'
         WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'
         ELSE 'Over $1000'
      END
FROM Production.Product
ORDER BY ProductNumber ;

2 Comments

No it seems clear he wants the Label repeated as many times as the Labels column.
@Vitaliy this is not what he pretended, not an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.