distinct comma separated values

  • Hi Friends,

    Need small requirement

    CREATE TABLE MST_GENDER

    (YR INT,

    GENDER_DESC VARCHAR(100)

    )

    INSERT INTO MST_GENDER

    SELECT 2003,'M'

    UNION ALL

    SELECT 2003,'F'

    UNION ALL

    SELECT 2004,'M'

    UNION ALL

    SELECT 2004,'F'

    UNION ALL

    SELECT 2004,'O'

    -- NOW I WOULD LIKE TO ASSIGN DISTINCT VALUES OF GENDER IN A VARIABLE WITH COMMA SEPARTED

    -- ALL GENDERS

    DECLARE @values VARCHAR(500)

    SELECT @values = ISNULL(@values+',','')+GENDER_DESC FROM MST_GENDER

    SELECT @values

    --M,F,M,F,O

    i tried with the below but giving different output

    DECLARE @values VARCHAR(500)

    SELECT distinct @values = ISNULL(@values+',','')+GENDER_DESC FROM MST_GENDER

    SELECT @values

    -- O

    Thanks in Advance

  • hI,

    Also Try this

    DECLARE @values VARCHAR(500)

    SELECT @values = coalesce(@values+',','')+GENDER_DESC

    FROM MST_GENDER

    GROUP BY GENDER_DESC

    SELECT @values

  • Hi Arun,

    Thank You.:-)

Viewing 4 posts - 1 through 3 (of 3 total)

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