Looks like when you want to actually search for an _ or % you have to tell SQL
what character you want to use as an escape. I couldn't find anything that
said what the default escape character is so maybe this is the only way to do
it.
Anyway here is what works
select node_name from nodes where node_name like '%\_%' escape '\'
When you use the word escape followed by quotes and any character in the quotes
that character will be treated like an escape character. So the following will
work also
select node_name from nodes where node_name like '%!_%' escape '!'
google is my friend I searched for "escape character for SQL selects" and the
first hit was an example. I then tested it and it appears to work. The page
that came up was <http://www.techonthenet.com/sql/like.htm. This page had a
lot of good information where it explains
Kyle
James R Owen <Jim.Owen AT YALE DOT EDU> wrote:
[Hey:
If you're really in a hurry, skip to the next page, and see <(@)>,
but you might miss some fun(ny, perhaps even useful?) stuff!]
I just accidentally discovered [!much to my surprise!] that...
The underscore "_" character is the SQL SELECT standard "wildcard"
to match "any single character in the specified position", e.g.,
select node_name from nodes where node_name like '_A_'
[lists all nodes w/ names 3 characters long w/ 'A' in the middle.
[Probably] we all[ready] know [or have discovered] that...
The percent "%" character is the SQL SELECT standard "wildcard"
to match "zero or more characters" [but NOT a NULL string!]
select node_name from nodes where node_name like '%-%'
[lists all nodes w/ hyphenated names, and also bizarre names
like '-', '+-', '1-0', '.-.', '...---...' (morse code!), etc.]
Now, what I really want is a list of nodes w/ names including
a literal underscore "_" character so I first tried...
select node_name from nodes where node_name like '%_%'
[which seems to be exactly equivalent to...]
select node_name from nodes where node_name like '%'
[Both list all existing nodes (because node_name can't be null!)]
Then I borrowed an Oracle DBA's SQL ref.manual and found...
The backslash "/" character is the SQL SELECT standard "escape" used
preceding a wildcard to match that (following) character literally.
<(@)> So, I tried again...
tsm: TST>select node_name from nodes where node_name like '%\_%'
ANR2034E SELECT: No match found using this criteria.
ANS8001I Return code 11.
tsm: TST>select node_name from nodes where node_name like '%_OB'
NODE_NAME
------------------
xxxxxx_OB
yyyyy-OB
zzzzz_OB
aaa_OB
bbbb_OB
ccccOB
tsm: TST> [names partially changed to protect the innocent]
!!!What to do??? [Please, don't say there's no escape!]
ITSM 5.2.3.0 w/ AIX 5.2
--
Jim.Owen AT Yale DOT Edu (203.432.6693)
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
|