Oracle REGEXP_LIKE Strange Behavior - oracle11g

Have a simple query it's failing for one set of parameters but it works for other parameters it's not working.
This works:
SELECT R.*
FROM ROUTEUSER.AHC_B2B_ROUTE R
WHERE R.PRODUCER = 'Encounters'
AND REGEXP_LIKE ('tplmcoce.41.20170822.txt', R.FILEMASK, 'i')
This is not working
SELECT R.*
FROM ROUTEUSER.AHC_B2B_ROUTE R
WHERE R.PRODUCER = 'Facets'
AND REGEXP_LIKE ('SMS-0162628062', R.FILEMASK, 'i')
If I have a column called Filemask (REGEX Pattern) in database so how can I select matching pattern for given string (file name)?
When I try the second query I am getting the following exception:
ORA-12725: unmatched parentheses in regular expression 12725. 00000 - "unmatched
parentheses in regular expression"
*Cause: The regular expression did not have balanced parentheses.
*Action: Ensure the parentheses are correctly balanced.

SELECT R.*
FROM ROUTEUSER.AHC_B2B_ROUTE R
WHERE R.PRODUCER = 'Facets'
AND REGEXP_LIKE ('^SMS[-]0162628062$*', R.FILEMASK, 'i') ---- Try and let us know output......................

Here is a thought...
select *
from <your_table>
where regexp_count( FILEMASK, '\(' ) != regexp_count( FILEMASK, '\)' )
;
You should find at least one where PRODUCER = 'Facets' (and perhaps more?)
... although this may not be enough. Consider this regexp: '\(abc)' (where you meant '\(abc\)'). You want literal parentheses around the string abc. But you only escaped the opening parenthesis and forgot to escape the closing one. For a regular expression, that is an error: the escaped parenthesis is treated as any other character, it is not "special" in any way; this leaves the closing parenthesis as a special character, and it is not matched by an opening one.
On the other hand, my solution above is not able to distinguish between escaped and non-escaped parentheses - it treats them all the same, so '\(abc)' looks perfectly fine to it. If what I posted above is not enough, you/we will need more subtle ideas. Perhaps:
...
where regexp_count( FILEMASK, '([^\]|^)\(' ) != regexp_count( FILEMASK, '([^\]|^)\)'
This only looks for non-escaped parentheses. [^\] means "any character other than a backslash", and ([^\]|^) means either that or the beginning of the string.

This issue is resolved. This is because I had one bad regular expression (missed one parenthesis) which caused the failure. The REGEX_LIKE started comparing each mask in table column when it come to the bad regex it failed.
Please close this question.
Thanks all.

Escape the Chars in the [] with '\' . And you have to replace the last char '.' with * in the second pattern('SMS...')
^tplmcoce[\.](37|41|47|57)[\.].*[\.]txt$
and
^SMS[\-]0162628062$*
SELECT 1 Matched
FROM DUAL
WHERE REGEXP_LIKE ('tplmcoce.41.20170822.txt',
'^tplmcoce[\.](37|41|47|57)[\.].*[\.]txt$', 'i');
SELECT 1 Matched
FROM DUAL
WHERE REGEXP_LIKE ('SMS-0162628062', '^SMS[\-]0162628062$*', 'i')

Related

Teradata remove enclosing single quotes from variable

I need to replace single quotes in a string of numbers and use in a WHERE IN clause. for example, I have
WHERE Group_ID IN (''4532','3422','1289'')
The criteria within parenthesis is being passed as a parameter, so I have no control over that. I tried using :
WHERE Group_ID IN (REGEXP_REPLACE(''4532','3422','1289'', '[']', ' ',1,0,i))
also tried using OReplace
WHERE Group_ID IN (OReplace(''4532','3422','1289'', '[']', ' '))
but get the same error:
[Teradata Database] [3707] Syntax error, expected something like ','
between a string or a Unicode character literal and the integer '4532'.
Please suggest how to remove the single enclosing quotes or even removing all single quotes should work as well.
The string ''4532','3422','1289'' you are using is incorrect because it contains non-escaped single quotes. This is a syntax error in SQL. In this particular form, no matter what function you use to fix it or which RDBMS you use, it will result in error with standard SQL.
Functions in the SQL cannot fix syntax errors. REGEXP_REPLACE and OReplace never get executed because the query never enters the execution state. It never goes past the SQL syntax parser.
To see the error from perspective of the SQL parser, you may break the string in to multiple parts
'' -- SQL Parser sees this as a starting and ending quote and hence an empty string
4532 -- Now comes what appears to SQL parser as an integer value
',' -- Now this is a pair of quotes containing a single comma
3422 -- Again an integer
',' -- Again a comma
1289 -- Again integer
'' -- Again emtpy string
This amalgam of strings and numbers will not mean anything to the SQL parser and will result in an error.
Fix
The fix is to properly escape the data. Single quotes must be escaped using another preceding single quote. So correct string in this scenario becomes '''4532'',''3422'',''1289'''
Another thing is that the OReplace usage (once syntax is fixed) is like OReplace(yourStringValueHere, '''', ' ')) Observe the usage of escaped single quote here. Two outer quotes are for the string start and end. First inner quote is the escape character and second inner quote is the actual data passed to the function.

How to do a column name inside of a dynamic where clause? TO_NUMBER(column name)

I am currently trying to create a dynamic Select statement when the user has to input a various amount of criteria to search by.
Currently, I have every part of the statement working except for the most important part.
I am attempting to do something like this:
selStmt := 'SELECT column_one, column_2, column_3
FROM nerf
whereClause := ' WHERE TO_NUMBER('''|| column_one ||''') <= '''|| userInput ||'''';
However, in doing this the WHERE cluse of my SELECT statement is not accurate as shown by my output line:
WHERE TO_NUMBER('') <= '5';
I have tried various solutions with quote marks and I end up with either a ORA-00905 missing identifier error, or I get a ORA-00911: invalid character error.
At this point I'm not quite sure how to approach this issue.
Any useful help gets thanks in advance.
For some reason, Oracle uses the single quote to delimit strings and to escape characters, so using '' is an instruction to Oracle to add a quote inside your string. Example:
'This is a string with a quote here: '' and then it ends normally'
will be represented as
This is a string with a quote here: ' and then it ends normally
In your example, you are ending the WHERE clause you're building up and then concatenating a PL/SQL variable identifier called column_one:
' WHERE TO_NUMBER('''|| column_one ||''')
...and with a NULL value for the identifier column_one this is represented as
WHERE TO_NUMBER('')
Presumably you want to reference column_one from inside the query, and not from a PL/SQL variable of the same name, so you should remove the quotes around it like so:
whereClause := ' WHERE TO_NUMBER(column_one) <= TO_NUMBER('''|| userInput ||''')';
Escaping strings in Oracle is often infuriating - it helps a lot if you have a good IDE with decent syntax highlighting like TOAD or SQL*Developer.
This should work:
selStmt := 'SELECT column_one, column_2, column_3 FROM nerf';
whereClause := ' WHERE TO_NUMBER(column_one) <= TO_NUMBER('''|| userInput ||''')';

SQLite: How to select part of string?

There is table column containing file names: image1.jpg, image12.png, script.php, .htaccess,...
I need to select the file extentions only. I would prefer to do that way:
SELECT DISTINCT SUBSTR(column,INSTR('.',column)+1) FROM table
but INSTR isn't supported in my version of SQLite.
Is there way to realize it without using INSTR function?
below is the query (Tested and verified)
for selecting the file extentions only. Your filename can contain any number of . charenters - still it will work
select distinct replace(column_name, rtrim(column_name,
replace(column_name, '.', '' ) ), '') from table_name;
column_name is the name of column where you have the file names(filenames can have multiple .'s
table_name is the name of your table
Try the ltrim(X, Y) function, thats what the doc says:
The ltrim(X,Y) function returns a string formed by removing any and all characters that appear in Y from the left side of X.
List all the alphabet as the second argument, something like
SELECT ltrim(column, "abcd...xyz1234567890") From T
that should remove all the characters from left up until .. If you need the extension without the dot then use SUBSTR on it. Of course this means that filenames may not contain more that one dot.
But I think it is way easier and safer to extract the extension in the code which executes the query.

Removing trailing Spaces from Long Datatype PL/SQL

I have a Long with a couple of sentences in it, at the end there is a huge amount of blank spaces that need removed.
The problem is that the I have wrote a function to convert this long to a Varchar2 and trim the spaces but this has not worked.
I have used, RTRIM, TRIM TRAILING, TRIM and even tried replace " " with "" (but that just removed all spaces even between words.
Example:
SELECT TRIM(comment)
FROM p_comments
WHERE p_domain = 'SIGNATURE'
AND p_code = c_p_code;
This did not work as it cannot perform the trim on a "LONG".
SELECT RTRIM(f_get_varchar(get_p_code('JOHN'))) FROM dual
Did not work and just returned the same result.
Does anyone have any ideas?
Managed to find the answer. I used a regular expression.
SELECT regexp_substr(cis.acs_reports.f_get_varchar(:p_pfo_code), '.+[^space::]') pfo_comment
FROM dual

SQLite query - using [] in SQLite queries

I would like to know if it is possible to use [] in SQLite query as we used to in Access and other DB.
e.g. SELECT * FROM mytable WHERE fwords like '%b[e,i,a]d%'
this will retrieve all rows have fwords containing bad, bed, bid
Thanks a lot
From http://www.sqlite.org/lang_expr.html:
The LIKE operator does a pattern matching comparison. The operand to the right of the LIKE operator contains the pattern and the left hand operand contains the string to match against the pattern. A percent symbol ("%") in the LIKE pattern matches any sequence of zero or more characters in the string. An underscore ("_") in the LIKE pattern matches any single character in the string. Any other character matches itself or its lower/upper case equivalent (i.e. case-insensitive matching).
Does that help?
You can have a look at the regex section here.

Resources