Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 simple select from csv

Author  Topic 

ad5qa
Starting Member

2 Posts

Posted - 2008-01-28 : 21:46:29
I have a simple select statement that I am having a complex with. I am a C# developer trying to expand and gain a little more knowledge of SQL. I want to send a string to a SQLCommand catagory = 'news,alerts,events' and return a result set if it matches any term.

This is for a blog I am creating for a church and the catagory is like the tags you normally see. I could use something like:

SELECT GUID,CATAGORY,DESCRIPTION,TITLE,ETC
FROM RSS_FEEDS
WHERE CATAGORY LIKE 'news,alerts,events'

I can seperate the keywords with any character if needed.

WHERE (ITEM_CATEGORY LIKE '%news,events%')

This only returns ones that have all like what is there not the ones having news only

Is there a way or trick to this.

Thanks


tprupsis
Yak Posting Veteran

88 Posts

Posted - 2008-01-28 : 21:57:07
Not sure if your problem is hooking up to the CSV, parsing the parameter list, or the actual select statement. It looks like your question is about the select statement. To get records where the category is like news or like alerts, you can use the following:

WHERE (ITEM_CATEGORY LIKE '%news%') OR (ITEM_CATEGORY LIKE '%events%')

Hope that helps.

Tom Rupsis
Granite Peak Systems
Phone: 406-672-8292
Email: trupsis@granitepeaksys.com
LinkedIn: www.linkedin.com/in/trupsis
Go to Top of Page

ad5qa
Starting Member

2 Posts

Posted - 2008-01-28 : 22:14:50
That will totally work, I didn't think (forgot) about the OR clause.

I am actually looing for the best way to execute the lookup of tags from the website. The user will click on checkboxes like news, events, alerts if they want to see those RSS catagories. So I am sending the catagories in a string to the SQLCommand. With what you provided I will simple loop through and append until I find a better way.

string sql = "SELECT GUID,CATAGORY,DESCRIPTION,TITLE,ETC ";
sql += "FROM RSS_FEEDS WHERE ";

foreach(string str in array)
{
sql += " (ITEM_CATEGORY LIKE '%" + str + "%') ";
if (LASTONE THEN DO NOT ADD 'OR' TO THE END)
}

This could end up big depending on how many tags there will be:

SELECT GUID,CATAGORY,DESCRIPTION,TITLE,ETC
FROM RSS_FEEDS
WHERE (ITEM_CATEGORY LIKE '%news%') OR
(ITEM_CATEGORY LIKE '%alerts%') OR
(ITEM_CATEGORY LIKE '%events%') OR
(ITEM_CATEGORY LIKE '%events%') OR
(ITEM_CATEGORY LIKE '%teens%') OR
(ITEM_CATEGORY LIKE '%prayer%')

But I think SQL Server can hangle it. I may make it into a SP.

Thanks for the help

Go to Top of Page
   

- Advertisement -