October 21, 2010 at 8:14 am
Hi All,
i have list of 80 words where i want to use like
ex my query is like this
select * from xzy where column like '%abc%' or column like '%asd%' or column like '%ad%' or column like '%ahd%' ............ 80 times
instead of writing 80 times is there any other way that i can do ??
Thanks in Advance
October 21, 2010 at 8:19 am
Good question. I can't think of an answer.
Why do you need to do this? If you tell us what you're trying to achieve, maybe we can come up with an alternate solution.
+--------------------------------------------------------------------------------------+
Check out my blog at https://pianorayk.wordpress.com/
October 21, 2010 at 8:25 am
i have to select a column from a table which has all these 80 words so i am planning to write this using LIke.
please suggest me if you any alternate way
Thanks in Advance
October 21, 2010 at 8:27 am
Maybe regular expressions?
here is a link to a codeplex project which may help get you started.
http://www.codeproject.com/KB/database/SqlRegularExpressions.aspx
It may be more work though....
October 21, 2010 at 8:42 am
Here's a sample of how you could do it:
-- Table to hold the words to search for
DECLARE @Patterns TABLE (
search_pattern varchar(50)
)
-- Table to hold the text to search in
DECLARE @test-2 TABLE (
test_text nvarchar(max)
)
-- Populate the test table with the text of the
-- stored procedures in database msdb
INSERT INTO @test-2
SELECT definition
FROM msdb.sys.sql_modules
-- Define some patterns to search for
INSERT INTO @Patterns VALUES ('sysmail')
INSERT INTO @Patterns VALUES ('jobserver')
INSERT INTO @Patterns VALUES ('schedule')
-- Select out the stored procedures that match any of the patterns
SELECT *
FROM @test-2 AS A
WHERE EXISTS (
SELECT 1
FROM @Patterns AS B
WHERE A.test_text LIKE '%' + B.search_pattern + '%'
)
Hope this helps
Gianluca
EDIT: changed INNER JOIN to EXISTS to avoid duplicate rows when more than one pattern is matched
-- Gianluca Sartori
October 21, 2010 at 8:52 am
yep that worked Gianluca
Thanks everyone for your response
October 21, 2010 at 8:55 am
kancherla.ks (10/21/2010)
i have to select a column from a table which has all these 80 words so i am planning to write this using LIke.please suggest me if you any alternate way
That doesn't answer the below question.
Ray K (10/21/2010)
Why do you need to do this? If you tell us what you're trying to achieve, maybe we can come up with an alternate solution.
We understand your list for your like statement, but what are you comparing it to? Why do you need to make this comparison? What type of application or report are you doing and what does the data you're search against look like?
What about the table and indexes you're executing these Like statements against? What's the structure?
Answer all these questions and we can give you a better answer to your original post.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply