Need Help with a Case-Sensitive 'Like' Query

C

CL

I want to query a number of database fields to return only those
records that contain a specific string, and I want the match to be
case-sensitive.

I am currently using 'LIKE' in a query that looks something like this:

SELECT *
FROM tblArtistInfo
WHERE tblArtistInfo.ArtistName LIKE '%Joe Blow%'

This query returns results when it encounters 'Joe Blow' or 'Joe blow'.

How do I make sure my results are case sensitive and only return
matches for 'Joe Blow' and NOT 'joe blow', 'Joe blow', or 'joe Blow'?
 
K

Ken Snell [MVP]

You'll need to use a criterion that contains the StrComp function, using the
binary comparison setting. Check it out in Help file.
 
J

John Vinson

I want to query a number of database fields to return only those
records that contain a specific string, and I want the match to be
case-sensitive.

I am currently using 'LIKE' in a query that looks something like this:

SELECT *
FROM tblArtistInfo
WHERE tblArtistInfo.ArtistName LIKE '%Joe Blow%'

This query returns results when it encounters 'Joe Blow' or 'Joe blow'.

How do I make sure my results are case sensitive and only return
matches for 'Joe Blow' and NOT 'joe blow', 'Joe blow', or 'joe Blow'?

You'll need to use the InStr function with its optional Compare
operand:

SELECT *
FROM tblArtistInfo
WHERE InStr(1, [tblArtistInfo].[ArtistName], "Joe Blow", 0) > 0;

See the VBA online help for InStr.

John W. Vinson[MVP]
 
C

CL

Thanks for the help!
John your method seems to work just fine.

I found more details on the InStr function here:
MSDN Home > MSDN Library > Web Development > Scripting > Windows
Script Technologies > VBScript > Reference > Functions

Do you recommend any other resources? I'd like to learn the difference
between the binary and text comparisons InStr uses.
 
J

John Vinson

Do you recommend any other resources? I'd like to learn the difference
between the binary and text comparisons InStr uses.

I meant to say *offline* help... <g> Just the Help file topic for
Instr, but I would guess you got the same content.

Binary comparison is bit by bit, so that N and n and ñ are all
distinct characters. Character comparison collapses case and uses the
Windows language settings to decide which characters are "the same".

John W. Vinson[MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top