?
Solved

SQL LIKE clause matches underscore?

Posted on 2007-04-03
14
Medium Priority
?
7,140 Views
Last Modified: 2012-05-05
I have a SQL SELECT statement that has a clause:
  "WHERE filename LIKE " & sFileSpec
and when sFileSpec is "4_03" it returns a match for filename = "4303". Does the underscore character act like a wildcard, or does the LIKE clause match strings that only differ by one character, or is there another reason that it returns a match?
0
Comment
Question by:lee88
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +5
14 Comments
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 18846735
change this:
          "WHERE filename LIKE " & sFileSpec
To:
 "WHERE filename LIKE '" & sFileSpec & "'"

to do a text match
0
 
LVL 38

Accepted Solution

by:
Jim P. earned 2000 total points
ID: 18846737
The underscore is a single wildcard chararacter like the ? in Access queries.

"WHERE filename LIKE " & Replace(sFileSpec,"_", "\_"  & " ESCAPE '\'"

The ESCAPE clause tells SQL to treat the character after the backslash as a regular character, not a wildcard.
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 18846741
Missed a closing paren.
"WHERE filename LIKE " & Replace(sFileSpec,"_", "\_")  & " ESCAPE '\'"
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 9

Expert Comment

by:TheSloath
ID: 18846744
_ is the same as ? in a like statement: it is a placemarker for any character.

Try "4[_]03"
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 18846747
Underscore is a wildcard character.  It matches any one character in that position in the string.  To look for an underscore, place brackets around the underscore, e.g.:

WHERE filename LIKE " & Replace(sFileSpec, "_","[_]")

This would leave the string value at 4[_]03
0
 
LVL 16

Expert Comment

by:rboyd56
ID: 18846749
Based on Books on Line the underscore acts like a single charaacter:

WHERE au_fname LIKE '_ean' finds all four-letter first names that end with ean (Dean, Sean, and so on).
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18846752
does the length of sFileSpec varies ot it is a 4 character string?


"WHERE Left(filename,4)='" & sFileSpec & "'"
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18846764
look at that 4 replies at the same time....
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 18847067
Glad to be of assistance. May all your days get brighter and brighter.
0
 
LVL 22

Expert Comment

by:neeraj523
ID: 23835931
This solution did not work out for me,wonder why?
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 23836799
Hit the "ask a related question" link above and give us details. We should be able to help.
0
 
LVL 22

Expert Comment

by:neeraj523
ID: 23843696

guys.. what is this about ?? dont understand 'Open Discussion' section
0
 
LVL 22

Expert Comment

by:neeraj523
ID: 23843699


"neeraj523
03.09.2009 at 07:30PM IST, ID: 23835931
This solution did not work out for me,wonder why?"

I never posted this comment..
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 23846465
"Open Discussion" means this is being posted after the question is closed, so no points can be assigned.

neeraj523, if you didn't post something ascribed to you, I suggest you change your password.  Also, if co-workers have access to your computer, lock it when you step away from your desk.  There have been some jokes around my office based on failure to do that ...
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question