I came across code similar to the following in an Oracle stored procedure:
SELECT * FROM hr.employees WHERE REGEXP_LIKE(FIRST_NAME, '\A'||:iValue||'\Z', 'c');
And I am not sure what the \A and \Z do.
From what I can glean from the Oracle documentation, I think that they simply suppress the meaning of special characters in the iValue parameter. If so, the above must be equivalent to
SELECT * FROM hr.employees WHERE FIRST_NAME=:iValue;
Can anyone confirm this? Empirically this seems to be the case.
I think that in the past they wanted case insensitive searching so the 'c' was an 'i' before. So in this case we do not need to use the REGEXP_LIKE function any more and can replace it with an equals.
\A matches the position at the beginning of the string.
\Z matches the position at the end of the string or before a newline at the end of the string.
\z matches the position at the end of the string.
These are independent of multiline mode, unlike ^ and $.
Example:
foo\Z would match on foo\n, but foo\z would not match on foo\n.
See Oracle reference.
if || is used for string concatenation, then it's not the same as simple string comparison as it would allow you to use regex. (Also I'm not sure how Oracle treats case sensitivity when using =, MySQL ignores case by default when comparing strings.)
\A matches the very start of input.
\Z matches the very end of input.
Check out regular-expressions.info, which is a great regex resource
Related
we are using following SQL to identify several rows which contain only SPECIAL CHARACTERs 'F#C(!!'. But seems it's not capturing some SPECIAL CHARACTER like 'F#C(!!'. Script should capture only special characters. Could you please share the optimized script to use for this scenario case
when regexp_like(column_name '^[^a-zA-Z]*$') then 'number'
when regexp_like(column_name, '^[^g-zG-Z]*$') then 'hex'
else 'string'
end
Try removing the ^ and $ anchors from the regex pattern you are using with regexp_like:
select *
from your_table
where regexp_like(column_name, '[^a-zA-Z]');
The above logic would check for the presence of one or more characters which are not letters. If you instead want to check for non alphanumeric, then use [^A-Za-z0-9].
I want to judge if a positive number string is end with ".0", so I wrote the following sql:
select '12310' REGEXP '^[0-9]*\.0$'. The result is true however. I wonder why I got the result, since I use "\" before "." to escape.
So I write another one as select '1231.0' REGEXP '^[0-9]\d*\.0$', but this time the result is false.
Could anyone tell me the right pattern?
Dot (.) in regexp has special meaning (any character) and requires escaping if you want literally dot:
select '12310' REGEXP '^[0-9]*\\.0$';
Result:
false
Use double-slash to escape special characters in Hive. slash has special meaning and used for characters like \073 (semicolon), \n (newline), \t (tab), etc. This is why for escaping you need to use double-slash. Also for character class digit use \\d:
hive> select '12310.0' REGEXP '^\\d*?\\.0$';
OK
true
Also characters inside square brackets do not need double-slash escaping: [.] can be used instead of \\.
If you know it is a number string, why not just use:
select ( val like '%.0' )
You need regular expression if you want to validate that the string has digits everywhere else. But if you only need to check the last two characters, like is sufficient.
As for your question . is a wildcard in regular expressions. It matches any character.
SQLITE Query question:
I have a query which returns string with the character '#' in it.
I would like to remove all characters after this specific character '#':
select field from mytable;
result :
text#othertext
text2#othertext
text3#othertext
So in my sample I would like to create a query which only returns :
text
text2
text3
I tried something with instr() to get the index, but instr() was not recognized as a function -> SQL Error: no such function: instr (probably old version of db . sqlite_version()-> 3.7.5).
Any hints howto achieve this ?
There are two approaches:
You can rtrim the string of all characters other than the # character.
This assumes, of course, that (a) there is only one # in the string; and (b) that you're dealing with simple strings (e.g. 7-bit ASCII) in which it is easy to list all the characters to be stripped.
You can use sqlite3_create_function to create your own rendition of INSTR. The specifics here will vary a bit upon how you're using
I have the following regex that checks for a list of valid characters:
^([a-zA-Z0-9+?/:().,' -]){1,35}$
What I now need to do now is search for any existing columns in our DB that invalidates the above regex. I'm using the oracle SQL REGEXP_LIKE command.
The problem I have is I can't seem to negate the above expression and return a value when it finds a character not in the expression e.g.
"a-valid-filename.xml" => this shouldn't be returned as it's valid.
"an_invalid-filename.xml" => I need to find these i.e. anything with an invalid character.
The obvious answer to me is to define a list of invalid characters... but that could be a long list.
You can match it against the following regex which uses the [^...] negation character class:
([^a-zA-Z0-9+?/:().,' -])
This will match any single character that is not part of the list of characters that are allowed.
You can negate a character class by inserting a caret as the first character.
Example:
[^y]
The above will match anything that is not y
Try this:
where not regexp_like(col, '^([a-zA-Z0-9+?/:().,'' -]){1,35}$')
or
where regexp_like(col, '[^a-zA-Z0-9+?/:().,'' -]')
I require a means of checking to see if a string has the following exact pattern within it, i.e.:
(P)
Examples where this would be true is:
Test System (P)
I am not sure though how to check for cases when the string that doesn't have '(P)', i.e:
'Test System (GUI for Prof)' - in this case, this would be false but I am using REGEXP_LIKE and it actually returns TRUE.
I only want it to return True when the exact string of '(P)' exists within the search string.
How can I do this in PL/SQL?
Use:
REGEX_LIKE(t.column, '\(P\)')
Regular-Expressions.info is a great resource.
Regular INSTR would work (Oracle 8i+):
WHERE INSTR(t.column, '(P)') > 0 --column contains '(P)'
WHERE INSTR(t.column, '(P)') = 0 --column does NOT contain '(P)'
LIKE works too:
WHERE t.column LIKE '%(P)%' --column contains '(P)'
WHERE t.column NOT LIKE '%(P)%' --column does NOT contain '(P)'
Try like:
WHERE thing like '%(P)%';
I would stick with REGEXP_* functions, as you'll need to practice them anyway, and knowing regular expressions will serve you well.
They're all good answers, except for a typo in Ponies' first answer. :
The typo is that there's a P missing from REGEX_LIKE:
Written: REGEX_LIKE(t.column, '\(P\)')
Correct: REGEXP_LIKE(T.COLUMN, '\(P\)')
The '\' is an escape character that says "don't look for the symbolic meaning of the next character, but look for the literal character itself."