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
 Like operator

Author  Topic 

sital
Yak Posting Veteran

89 Posts

Posted - 2009-04-13 : 10:38:41

Hi all,

I am a beginner in SQL server 2005. I am using the following query. Can any one give any other alternative to this query by which I cn avoid repeating the column name and the like operator?

select * from HumanResources.Department where Name like 'P%' or Name like 'Sal_' or Name Like 'Sal__' or Name like 'F[ie]%' or Name like 'F[^a]%'

Please note the column name is Name in the above query.

Please help me out with suitable alternative in which I can avoid writing the name of the column and the Like operator?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2009-04-13 : 12:39:09
You can put your search terms into a table and join on that table, like this:
declare @SearchTerms table  (term varchar(127));
insert into @SearchTerms (term) values ('P%');
insert into @SearchTerms (term) values ('Sal_');
insert into @SearchTerms (term) values ('Sal__');

select
*
from
HumanResources.Department d
inner join @SearchTerms s on d.name like s.term

It will need more work, depending on your data. For example, if a given row in your table will match two or more search criteria, you will get duplicate rows returned - if you don't want that, that will need to be fixed.
Go to Top of Page
   

- Advertisement -