Search 'Email address' in where clause in SQL

How to search multiple email address in below SQL query.....

------------------
Declare @EmailId varchar (max)
set @EmailId='anuradha.dutta@xyz.com,HEENA.SANOTRA@xyz.COM'

Declare @String4 varchar (Max)

Set @String4 ='select * from EmailSummary ES
where ES.'+@EmailIdWhere+'
----------------------------------------------------------------------------------
Payal sathavaraWindows Developer Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Show columns from your table EmailSummary ?
What column has email ids?
0
Payal sathavaraWindows Developer Author Commented:
Declare @EmailId varchar (max)
set @EmailId='anuradha.dutta@xyz.com,HEENA.SANOTRA@xyz.COM'

Declare @EmailIdWhere varchar (max)
Set @EmailIdWhere=Case when @EmailId='' then 'EMailAddressesTo<>''''' else 'EMailAddressesTo IN ('''+@EmailId+''')' end

Declare @String4 varchar (Max)

Set @String4 ='select * from EmailSummary ES
where ES.'+@EmailIdWhere+'
0
Pawan KumarDatabase ExpertCommented:
You need to try like below - Split and then join , IN clause will not work here.

--
Declare @EmailId varchar (max) =''
set @EmailId='anuradha.dutta@xyz.com,HEENA.SANOTRA@xyz.COM'

DECLARE @x AS XML=''
SET @x = CAST('<A>'+ REPLACE(@EmailId,',','</A><A>')+ '</A>' AS XML)

SELECT * FROM EmailSummary ES
INNER JOIN 
(
	SELECT t.value('.', 'VARCHAR(200)') Value FROM @x.nodes('/A') AS x(t)
)p ON p.Value = ES.EMailAddressesTo

--

Open in new window

0
Payal sathavaraWindows Developer Author Commented:
Dear Sir,
I have also search with accountID as well as Email then ?

Declare @AccountMasterId varchar (max)
set @AccountMasterId='495,12110'

Declare @AccountMasterIdsWhere varchar (max)
Set @AccountMasterIdsWhere=Case when @AccountMasterId='0' then 'AccountMasterId<>-1111' else 'AccountMasterId in ('+@AccountMasterId+')' end

DECLARE @x AS XML=''
SET @x = CAST('<A>'+ REPLACE(@EmailId,',','</A><A>')+ '</A>' AS XML)

      SELECT * FROM #EmailSummary ES LEFT OUTER JOIN AccountMaster A ON A.EMail = ES.EMailAddressesTo
                  INNER JOIN
                  (
                        SELECT t.value('.', 'VARCHAR(200)') Value FROM @x.nodes('/A') AS x(t)
                  )p ON p.Value = ES.EMailAddressesTo      


I  have join account master and search with AccountMasterID....How to set in Query ?
0
Pawan KumarDatabase ExpertCommented:
Please try like this -

Declare @AccountMasterId varchar (max) = ''
set @AccountMasterId='495,12110'

Declare @EmailId varchar (max) =''
set @EmailId='anuradha.dutta@xyz.com,HEENA.SANOTRA@xyz.COM'

DECLARE @x AS XML=''
SET @x = CAST('<A>'+ REPLACE(@EmailId,',','</A><A>')+ '</A>' AS XML)

DECLARE @y AS XML=''
SET @y = CAST('<A>'+ REPLACE(@AccountMasterId,',','</A><A>')+ '</A>' AS XML)

SELECT u.* FROM 
(
	SELECT * FROM #EmailSummary ES 
	INNER JOIN 
	(
		SELECT t.value('.', 'VARCHAR(200)') Value FROM @x.nodes('/A') AS x(t)
	)p ON p.Value = ES.EMailAddressesTo  
)u
LEFT OUTER JOIN 
(
	SELECT  * FROM AccountMaster A
	INNER JOIN 
	(
		SELECT t.value('.', 'VARCHAR(200)') Value FROM @y.nodes('/A') AS x(t)
	)o 
)A ON A.EMail = u.EMailAddressesTo

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.