0

MS SQL 2005. T-SQL.

I find many good solutions how split string. But how combine result from inner SELECT as string (for example, with comma-separator)? Code:

SELECT b.date,
(SELECT o.number FROM order o WHERE o.number = m.number ) AS orderList

FROM bank b, movemoney m

WHERE b.code = m.code

The table 'movemoney' consist of zero or few orders from the table 'order'. How create 'orderList' as string in one SELECT statement?

1 Answer 1

1

You can also put the XML PATH into a subquery in the SELECT if you want.
I just prefer this construct:

SELECT
    b.date,
    SUBSTRING(CAST(foo.bar AS varchar(8000)), 2, 7999) AS orderList
FROM
    bank b
    JOIN 
    movemoney m ON b.code = m.code
    OUTER APPLY
    (
    SELECT
        ',' + concatenatedid
    FROM
        order o
    WHERE
         o.number = m.number 
    FOR XML PATH ('')
    ) foo(bar)
4
  • Thanks. Is it possible create csv-string without JOIN? Commented Oct 1, 2011 at 16:47
  • JOIN is the SQL standard for almost 20 years
    – gbn
    Commented Oct 1, 2011 at 16:57
  • Andrey - Why you don't want to use ANSI SQL 92 JOIN syntax ? Commented Oct 1, 2011 at 16:58
  • My sql-query is very long, but monotonous. I dont't want to complicate it. Commented Oct 1, 2011 at 17:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.