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.
|between?SELECTindicates. Repeat theLabelQtyfieldLabelsnumber of times.*is not a valid operator for a string and this would result in a syntax error. Better to use English for this requirement.