String Aggregate

  • Is there a way to aggregate strings (concatenate), in a similar way as the Sum functions does with numbers.

     

  • Do you mean this

    SELECT 'this string' + ' ' + 'has been concatenated'

    or with variables

    DECLARE @string1 varchar(30),@string2 varchar(30)

    SET @string1 = 'this string'

    SET @string2 = 'has been concatenated'

    SELECT  @string1 + ' ' + @string2

    Far away is close at hand in the images of elsewhere.
    Anon.

  • You can write a UDF specific to the table and column, e.g.:

    USE Northwind

    GO

    CREATE FUNCTION dbo.f_ListCusts(@Country nvarchar(15)) RETURNS varchar(8000) BEGIN

    DECLARE @List varchar(8000)

    SELECT @List = ISNULL(@List + ',','') + CustomerID

    FROM dbo.Orders

    WHERE ShipCountry = @Country

    GROUP BY CustomerID -- makes sense in this case but probably should not be used in yours...

    RETURN @List END

    SELECT ShipCountry, dbo.f_ListCusts(ShipCountry)

    FROM dbo.Orders

    GROUP BY ShipCountry

    I suggest doing this at the presentation layer rather than the database back-end, though.



    --Jonathan

  • No I mean, to use the function in a similar way as the Aggregate function: SUM or COUNT or .....

     

     

  • Jonathan, as usual, in on the ball here. His solution is the answer to your question (I overlooked the word sum doh ). If not can you post more details, including table ddl, input and expected results.

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Give you give some sample data along with the desired result?

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Sorry Dave, haven't seen your response asking the same thing.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Thats OK Frank,

    Thought there was an echo

    there was an echo

    was an echo

    an echo

    echo

    echo

    FOTFL

     

    Far away is close at hand in the images of elsewhere.
    Anon.

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply