March 7, 2011 at 12:55 pm
user will click on a tab 'ABC','DEF'... and when they do i need to query for all last names that begin with A%, B%, C% or D%, E%, F% can i do this in one query or do i have to use 3 queries to retrieve all the rows that meet the criteria.
March 7, 2011 at 1:00 pm
You could use
WHERE yourColumn LIKE 'A%' OR yourColumn LIKE 'B%' OR yourColumn LIKE 'C%'
or just create a view with an additional column
CASE WHEN yourColumn LIKE 'A%' OR yourColumn LIKE 'B%' OR yourColumn LIKE 'C%' THEN 1
WHEN yourColumn LIKE 'D%' OR yourColumn LIKE 'E%' OR yourColumn LIKE 'F%' THEN 2
ELSE 3 END AS Grp
Then you could simply use WHERE Grp=1.
March 7, 2011 at 5:06 pm
Select LastName from MyTable
Where LastName like '[A-C]%'
Select LastName from MyTable
Where LastName like '[D-F]%'
or
Select LastName from MyTable
Where LastName like '[ABC]%'
Select LastName from MyTable
Where LastName like '[DEF]%'
...
It would be good to have an index on LastName. Check execution plan and logical reads.
Hope this helps.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply