For example, I have a table name test_table and has a column named column_A which has values:
A_1
A_2
A_3
A1
A2
A3
B_1
B_2
B_3
B1
B2
B3
If i want to select all the data has A_ as the beginning, i can use escape \ like:
select * from test_table where column_A like 'A\_*' escape '\';
so that _ not treated as a single character wildcard. i can get A_1, A_2 and A_3.
How should i do when i want use this in Between and operator? like
select * from test_table where column_A between 'A_\*' and 'B_\*'
i tried the above one, it didn’t escape the _. if i add a escape right after condition like
select * from test_table
where column_A between 'A_\*' escape '\' and 'B_\*' escape '\'
or
select * from test_table
where column_A between 'A_\*' and 'B_\*' escape '\'
i got a syntax error.
BETWEENdoesn’tseem topermitescaping ofwildcardsusing that syntax; I assume you’re getting ORA-00933: SQL command not properly ended? (Always useful to actually state the error you’re seeing).It does though allow the alternative syntax:Edit Actually although the syntax is allowed, it isn’t doing anything;
BETWEENisn’t treating_as a wildcard anyway, though it does treat%as one.Edit 2 As @Allan pointed out, it really isn’t treating
%as a wildcard, that’s just how the character ordering works.I’m not sure that’s what you actually want though, as it will give you
A_1, A_2, A_3, B1, B2, B3(at least with my NLS parameters).If what you actually want is
A_1, A_2, A_3, B_1, B_2, B_3then you can useREGEXP_LIKEinstead, something like:(though I’ll happily defer to others on the best way to construct that).