views:

39

answers:

2

hi,

how can i write the store procedure for searching particular string in a column of table, for given set of strings (CSV string).

like : select * from xxx where tags like ('oscar','rahman','slumdog')

how can i write the procedure for that combination of tags.

A: 

First off, the use of like for exact matches is sub-optimal. Might as well use =, and if doing so, you can use the IN syntax:

select * from xxx 
where tags IN ('oscar', 'rahman', 'slumdog') 

I am guessing you are not looking for an exact match, but for any record where the tags field contains all of the tags.

This would be something like this:

select * from xxx 
where tags like '%oscar%' 
  and tags like '%rahman%'
  and tags like '%slumdog%' 

This would be not be very fast or performant though.

Think about moving this kind of logic into your application, where it is faster and easier to do.

Edit:

Following the comments - there are lots of examples on how to parse delimited strings out there. You can put these in a table and use dynamic sql to generate your query.

But, this will have bad performance and SQL Server will not be able to cache query plans for this kind of thing. As I said above - think about moving this kind of logic to application level.

Oded
ok it is working fine but i have a doubt. we don't know how many string will give the user how can i write procedure for that
Surya sasidhar
in the above i give oscar,rahman,slumdog . if the user will give any number of string at that time how can we write store procedure for that
Surya sasidhar
You want to parse a comma separated string and search on that?
Oded
yes exactly Mr. Oded that is what i want
Surya sasidhar
A: 

To create a comma seperated string...

You could then apply this list to Oded example to create the LIKE parts of the WHERE cluase on the fly.

DECLARE @pos int, @curruntLocation char(20), @input varchar(2048)
SELECT @pos=0
SELECT @input = 'oscar,rahman,slumdog'
SELECT @input = @input + ','

CREATE TABLE #tempTable (temp varchar(100) )

WHILE CHARINDEX(',',@input) > 0
BEGIN
SELECT @pos=CHARINDEX(',',@input)
SELECT @curruntLocation = RTRIM(LTRIM(SUBSTRING(@input,1,@pos-1)))
INSERT INTO #tempTable (temp) VALUES (@curruntLocation)
SELECT @input=SUBSTRING(@input,@pos+1,2048)
END

SELECT * FROM #tempTable
DR0P TABLE #tempTable
kevchadders
Mr. Kevchadders i modify like the procedure like thiscreate procedure scrap (@string varchar(200))as DECLARE @pos int, @curruntLocation char(20), @input varchar(2048)SELECT @pos=0SELECT @input = @stringSELECT @input = @input + ','CREATE TABLE #tempTable (temp varchar(100) )WHILE CHARINDEX(',',@input) > 0BEGINSELECT @pos=CHARINDEX(',',@input)SELECT @curruntLocation = RTRIM(SUBSTRING(@input,1,@pos-1))INSERT INTO #tempTable (temp) VALUES (@curruntLocation)SELECT @input=SUBSTRING(@input,@pos+1,2048)END select * from videos where tags in( select * from #temptable)
Surya sasidhar
when i run the procedure it is showing no column can u help mei chage some code in the button of ur code can u look i send it the previous code thank you
Surya sasidhar
dont forget to drop the temp table, and test to make sure data exists in the #temptable before you select from the vidoes table. e.g. place a SELECT * FROM #tempTable before Select * from Vidoes...
kevchadders
also i edited the SQL to add a LTRIM to the statement to make sure spaces on both sides where removed. Make sure you apply that as well. ps. i have to place a zero in dr0p table due to that command being blocked on my side. (you will need to replace 0 with o)
kevchadders