query list into a string

Posted on 2006-04-04
Last Modified: 2010-03-19
I have a query that brings back a list of email address based on some factor I need to creat a list;;
from a result set
with out haveing to do a cursor
Question by:LeeHopkins
    LVL 15

    Expert Comment

    do something like this:

    declare @list varchar(1000)

    select @list = @list + emailaddress + ';' from table1

    set @list = substring(@list,1, (len(@list) - 1))

    Author Comment

    no go

    declare @list varchar(1000)

    select @list = @list + p_emailaddress + ';' from Purge_Email

    set @list = substring(@list,1, (len(@list) - 1))

    it should have returned 4 records

    Author Comment

    declare @list varchar(1000)
    select @list = Coalesce(@list + ', ', '') + p_emailaddress
    from purge_email where charindex('input_output',P_customer)> 0
    select @list

    Almost works but not giving distinct
    LVL 15

    Accepted Solution

    declare @list varchar(1000)
    select @list = Coalesce(@list + ', ', '') + distinctaddr.p_emailaddress
    from (select distinct p_emailaddress from purge_email where charindex('input_output',P_customer)> 0) as distinctaddr
    select @list

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    MS SQL page split per second is high 19 39
    Dynamic Database connection in SSIS 2012 11 42
    C# primary key 9 46
    sql sproc 2 22
    I recently came across an interesting Question In EE ( and was puzzled about how to achieve that using SSIS out of the box tasks, which was i…
    In this article—a derivative of my blog post (—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
    Via a live example, show how to setup several different housekeeping processes for a SQL Server.
    Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now