Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

select multiple wild cards... Expand / Collapse
Author
Message
Posted Monday, March 7, 2011 12:55 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, July 18, 2016 7:47 AM
Points: 78, Visits: 184
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.
Post #1074422
Posted Monday, March 7, 2011 1:00 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, February 10, 2016 11:50 AM
Points: 6,897, Visits: 13,559
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.




Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #1074428
Posted Monday, March 7, 2011 5:06 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, July 11, 2016 7:38 AM
Points: 370, Visits: 1,248
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.


_____________________________________________________
Microsoft Certified Master: SQL Server 2008
XDetails Addin - for SQL Developers
blog.sqlxdetails.com - Transaction log myths
Post #1074545
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse