Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi expert,

I am using sql server 2005.There are 2 tables.

emailinfo and keywordinfo.

emailinfo conatains id and email.

while keyword contains id,keyword.

Now i have to retrieve email from emailinfo table which are not containing all keywords from keywordinfo table.

Means i have to skip emails that are containing specific keyword.


Please help me for this.

Thanks in advnce.
Posted
Updated 20-Jan-12 20:04pm
v3
Comments
Amir Mahfoozi 20-Jan-12 23:48pm    
Give a short example.
udusat13 21-Jan-12 1:46am    
Thanks.

suppose there are keywords like webmaster,webcom,master etc in keyword column in keywordinfo table .
And i want to retrive all the email from emailinfo table which not containing these keywords from keywordinfo table.

its same like simple like query.

eg
select email from emailinfo where email not like '%webmaster%'

but here i wants to skip all the emails based on keywords from that table(keywordinfo).

I am searchin for query like,

select email from emailinfo where email not like '%' (select keyword from keywordinfo) '%'

1 solution

SQL
CREATE TABLE EmailInfo(
	ID INT IDENTITY(1,1),
	emails VARCHAR(MAX)
)

CREATE TABLE KeywordInfo (
	ID INT IDENTITY(1,1),
	keywords VARCHAR(MAX)
)

INSERT INTO KeywordInfo VALUES('webmaster')
INSERT INTO KeywordInfo VALUES('webcom')
INSERT INTO KeywordInfo VALUES('master ')

INSERT INTO EmailInfo VALUES('Email FOr WebMaster')
INSERT INTO EmailInfo VALUES('Email For Web')
INSERT INTO EmailInfo VALUES('Email FOr Master')
INSERT INTO EmailInfo VALUES('Email FOr WebCom')
INSERT INTO EmailInfo VALUES('Email FOr tejas')


DECLARE @QUERY VARCHAR(MAX) = ''
DECLARE @SELECT VARCHAR(MAX) = 'SELECT emails FROM EmailInfo WHERE emails '
DECLARE @WHEREFiled VARCHAR(MAX) = ' AND emails '
DECLARE @LIKE VARCHAR(MAX) = ' NOT LIKE '
DECLARE @LASTLike VARCHAR(MAX) = ' NOT LIKE '''''
DECLARE @Temp VARCHAR(MAX) = ''
SELECT @Temp = @Temp + @LIKE + '''%'+keywords+'%''' + @WHEREFiled  FROM KeywordInfo
SET @QUERY = @SELECT + @Temp + @LASTLike
EXEC(@QUERY)



here i have demonstrat a dynamic sql you can modify it using your fileds and values
 
Share this answer
 
Comments
udusat13 23-Jan-12 1:41am    
Thanks For replying me.

Your solution is helpful for me for other stored procedures also...

Thanks Again.
Tejas Vaishnav 29-Jan-12 23:10pm    
your wellcome.. for any query...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900