Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with putting results into a string

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I have this code but it only contains the last email.
I want to get everyones email in a string to send them email notifications.
like so
fred@nowhart.com, sue@gert.com, gerry@not.com

I was hoping to do this without looping using a cursor
Code:
DECLARE @results VarChar(1000)
SELECT @results = CASE
     WHEN @results IS NULL THEN CONVERT( VarChar(20), [email])
     ELSE ', ' + CONVERT( VarChar(20), [email])
   END
FROM dbo.SOWEmailManagersHistory 

Select @results

DougP
 
Sorry the code I got is junk, I don't want to strip out 20 characters
still need fred@nowhart.com, sue@gert.com, gerry@not.com

DougP
 
OK so now I have this code that I want to get into one string...
but it errors saying:

Code:
	Declare  @Today  Date
	Declare  @Monday  Date
	Declare @MondayCount int
	Declare @TuesdayCount int

	Set @Today = GETDATE()
	Set @Monday =(Select GETDATE() -1)
	
	SELECT STUFF( 
               (SELECT ', ' + [email]
                from SOWEmailManagersHistory where 
				convert(date,DateEntered,101)  = @Today
				Except
				Select Email from SOWEmailManagersHistory where 
				convert(date,DateEntered,101) = @Monday
               FOR XML PATH(''), TYPE).value('.', 'varchar(max)')
               ,1, 2, '')

DougP
 
Or try
Code:
	select REPLACE(
	( 
        SELECT 
            REPLACE(Email, CHAR(13), '') AS Email
        FROM 
            dbo.SOWEmailManagersHistory 
        FOR XML PATH ('')
    ), 
	' ', 
	', ')

MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA)
 
Try:

Code:
SELECT STUFF( (SELECT ', ' + t.[email]
               from SOWEmailManagersHistory as t
               where 
		   convert(date, t.DateEntered, 101)  = @Today and
                   not exists (select 1
			       from SOWEmailManagersHistory as m
                               where 
                                   m.[email] = t.[email] and
                                   convert(date, m.DateEntered, 101) = @Monday)
               FOR XML PATH(''), TYPE).value('.', 'varchar(max)')
               ,1, 2, '')

Hope this helps.

[URL unfurl="true"]http://www.imoveisemexposicao.com.br/imobiliarias-em-guarulhos[/url]
 
I think you're original query was close. How about

Code:
DECLARE @results VarChar(1000) = NULL
 SELECT @results = CASE WHEN @results IS NULL THEN email ELSE [red]@results[/red] + ', ' + email END
   FROM dbo.SOWEmailManagersHistory
 
A small change, to avoid adding a comma for every blank space:
Code:
select REPLACE(
	( 
        SELECT 
            REPLACE(Email, CHAR(13), '') + '*' AS Email
        FROM 
            dbo.SOWEmailManagersHistory 
        FOR XML PATH ('')
    ), 
	'*', 
	', ')

MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA)
 
declare @results varchar(max) = ''
select @results = @results + coalesce(email+', ','')
FROM dbo.SOWEmailManagersHistory
--strip last comma
set @results = substring(@results,1,len(@results)-1)
select @results

-----------
With business clients like mine, you'd be better off herding cats.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top