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.
| Author |
Topic |
|
rammohan
Posting Yak Master
212 Posts |
Posted - 2008-10-25 : 07:37:34
|
| Hi,I have A functionCREATE FUNCTION fn_Split_char(@text VARCHAR(MAX), @delimiter VARCHAR(20) = ' ')RETURNS@Strings TABLE ( [position] INT IDENTITY PRIMARY KEY , [value] varchar(100))ASBEGINDECLARE @index intSET @index = -1WHILE (LEN(@text) > 0)BEGIN -- Find the first delimiterSET @index = CHARINDEX(@delimiter , @text)-- No delimiter left?-- Insert the remaining @text and break the loopIF (@index = 0) AND (LEN(@text) > 0)BEGININSERT INTO @StringsVALUES (CAST(@text AS varchar(100)))BREAKEND-- Found a delimiter-- Insert left of the delimiter and truncate the @textIF (@index > 1)BEGININSERT INTO @StringsVALUES (CAST(LEFT(@text, @index - 1) AS varchar(100)))SET @text = RIGHT(@text, (LEN(@text) - @index))END -- Delimiter is 1st position = no @text to insertELSESET @text = CAST(RIGHT(@text, (LEN(@text) - @index)) AS varchar(100))END RETURNENDGO ANd Here is the code how i am executing it DECLARE @list VARCHAR(MAX)SELECT @list ='foo,bar,should not appear'DECLARE @testTable TABLE( [testId] INT , [value] VARCHAR(255) )INSERT @testTable ([testId], [value])SELECT 1, 'foo'UNIONSELECT 12412, 'bar'UNIONSELECT 22222222, 'should not appear'SELECT tt.*FROM @testTable ttJOIN fn_Split_char(@list, ',') filtON filt.[value] = tt.[value] From the above query ia m getting the results which contain value foo,bar and sholud not appear .I want to ask that How can i make these querie to return result alsowhen @list contains fo ,ba,not,appear,not appear,that means like condition also I want That if search wrd is fothan it can return row containg value foo and if kywrd is 'not' then it can return rowhaving value should not appear and if kywrd is 'not appear' then it can return rowhaving value should not appear . I dont want to do that with sql server full text search engine capability Plz Help Me.Thanks.One can never consent to creep,when one feels an impulse to soarRAMMOHAN |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-25 : 09:20:24
|
seems like this is what you wantSELECT tt.*FROM @testTable ttJOIN fn_Split_char(@list, ',') filtON tt.[value] LIKE '%' + filt.[value] + '%' |
 |
|
|
|
|
|
|
|