Coalesce / Comma Delimitted List

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • quizwedge
    New Member
    • Oct 2006
    • 5

    #1

    Coalesce / Comma Delimitted List

    I'm trying to return multiple column, one of which is a column + a comma delimitted list of values. Below is a simplified, non working, query:

    (assume @MyBit, @MyVarChar, and @MyValue are all declared)

    SELECT distinct
    IsNull(Column1, '') AS Column1,
    IsNull(Column2, '') + CASE WHEN @MyBit = 1 THEN ' My Test: ' + (select @MyVarChar = COALESCE(Column 3 + ', ', '') from TableSub where Value = @MyValue) ELSE '' END AS Column2,
    FROM TableMain

    I get an ADO error: Incorrect syntax near '='

    Thoughts?

    Thanks
  • scripto
    New Member
    • Oct 2006
    • 143

    #2
    (select @MyVarChar = COALESCE(Column 3 + ', ', '') from TableSub where Value = @MyValue)

    use "[ ]" brackets instead of "( )" around the above select statment.

    (and remove the last comma before the FROM stmnt)

    Comment

    • quizwedge
      New Member
      • Oct 2006
      • 5

      #3
      I now have:

      SELECT distinct
      IsNull(Column1, '') AS Column1,
      IsNull(Column2, '') + CASE WHEN @MyBit = 1 THEN ' My Test: ' + [select @MyVarChar = COALESCE(Column 3 + ', ', '') from TableSub where Value = @MyValue] ELSE '' END AS Column2
      FROM TableMain

      and I get:
      ADO error: Invalid column name 'select @MyVarChar = COALESCE(Column 3 + ',', '') from TableSub where Value = @MyValue'

      Thanks

      Comment

      • scripto
        New Member
        • Oct 2006
        • 143

        #4
        my apologies - here is the correct query

        SELECT distinct
        IsNull(Column1, '') AS Column1,
        IsNull(Column2, '') + CASE WHEN @MyBit = 1 THEN ' My Test: ' + (select COALESCE(Column 3 + ', ', '') from TableSub where Value = @MyValue) ELSE '' END AS Column2
        FROM TableMain

        put the "( )" back on and remove the @MyVarChar = (that is where the err is)
        you just need select coalesce...

        Comment

        Working...