I have a table with a column 'DESCRIPTION'.
I would like retrieve, by a regular expression, only rows with at least one lower case character.
I have tried
select * from MYTABLE t
WHERE REGEXP_LIKE (t.DESCRIPTION, '[a-z]');
but the result is equal to
select * from MYTABLE t
You may need to explicily force a case sensitive comparison:
select *
from MYTABLE t
WHERE REGEXP_LIKE (t.DESCRIPTION, '[a-z]', 'c')
From Oracle documentation:
If you omit match_parameter, then:
The default case sensitivity is determined by the value of the
NLS_SORT parameter
Related
I am using the below query to get Ids like 12766% and 39998%. How I can use regexp_like to check 12%66 and 39%98?
select * from table t where regexp_like( UPPER(ID),'12766|39998')
You may use the following regex pattern:
^(12[0-9]*66|39[0-9]*98)$
Oracle query:
SELECT *
FROM yourTable
WHERE REGEXP_LIKE(ID, '^(12[0-9]*66|39[0-9]*98)$');
Demo
But actually, you might just want to use LIKE here:
SELECT *
FROM yourTable
WHERE ID LIKE '12%66' OR ID LIKE '39%98';
This would work find, so long as you don't mind exactly what comes in between the digits.
I found solution for this. We can use the below query for a%d to match strings like abcd,acd,aabcd etc. A period character (.) is a perfect replacement for % in regexp which can support one or more occurrence of any characters supported in database.
select * from table where REGEXP_LIKE (UPPER (ID), '^12.66.|EY39.98.')
When is comparing strings case-sensitive and when case-insensitive in popular databases such as PostgreSQL, MySQL, MariaDB, Oracle, SQL Server, and SQLite?
I mean comparison using operators like: 'ab' = 'AB', or comparison of strings performed inside string functions like: POSITION('b' IN 'ABC'), INSTR('ABC', 'b'), REPLACE('ABC', 'b', 'x'), TRANSLATE('ABC', 'b', 'x'), TRIM('XabcX', 'x').
I think that I can know the answer but I don't know if it is correct.
Additionally, does the SQL standard define the case-sensitivity of string comparison?
Unfortunately, I only found a question about case-sensitivity of SQL syntax, and not in string comparison.
Edit: I am asking about default setting of the RDBMS, without additional setting the collation of a database, table, nor column.
I am asking only about ASCII letters A-Z and a-z.
Oracle DB functions/operators are case sensitive, with some exceptions including regexp_like method that can be switched to case-sensitive mode ;
--case sensitive demo
select q.txt
from (select 'myPhrase' txt from dual) q
where regexp_like(q.txt, 'myphrase'); --empty result set
select q.txt
from (select 'myPhrase' txt from dual) q
where q.txt like 'myphrase'; --empty result set
select q.txt
from (select 'myPhrase' txt from dual) q
where q.txt like 'myPhrase'; --exact match; returns value
select q.txt
from (select 'myPhrase' txt from dual) q
where regexp_like(q.txt, 'myphrase', 'i'); --case insensitive switch 'i'; returns value
select q.txt
from (select 'myPhrase' txt from dual) q
where lower(q.txt) like 'myphrase'; --enforced match; returns value;
Here's reference to docs for Collation rules for different SQL Operations and for Data Type Comparison Rules
Once a pseudo-collation is determined as the collation to use, NLS_SORT and NLS_COMP session parameters are checked to provide the actual named collation to apply
Usually these two are set to BINARY
SELECT
t.parameter,
t.value
FROM
nls_database_parameters t
WHERE
t.parameter IN (
'NLS_COMP', 'NLS_SORT'
);
I need to compare table counts for an Oracle schema to a SQL Server database. However, when I make my query, the results are always off because of the way each handles the underscore ('_') in terms of ordering. I've included an example of what I'm seeing below.
In Oracle:
SELECT FIELD1 FROM ORACLE_ORDER ORDER BY FIELD1 ASC;
Result:
'ABC'
'ABCD'
'ABC_D'
In SQL Server:
SELECT FIELD1 FROM SQL_ORDER ORDER BY FIELD1 ASC;
Result:
'ABC'
'ABC_D'
'ABCD'
As you can see from above, oracle and sql server both treat the underscore differently when it comes to ordering. How can I modify either of the queries (or environments) to make them order the same as the other?
In the SQL Server Side use the following
Select * from SQL_ORDER
ORDER BY FIELD1 Collate SQL_Latin1_General_CP850_BIN
The collation SQL_Latin1_General_CP850_BIN makes it to be used with ASCII values. In this case ASCII of underscore is 95, A being 65, and Z being 90. Remember lower case "a" will have a higher value than upper case "A" and so on.
Here is the fiddle
Simple way is to use Collate SQL_Latin1_General_CP850_BIN function in ORDER BY to achieve this
SELECT * FROM (
SELECT 'ABC' AS TAB UNION
SELECT'ABC_D'UNION
SELECT'ABCD'UNION
SELECT'ABC_'UNION
SELECT 'ABC' UNION
SELECT'A_C' UNION
SELECT'ABC_DE_FGH'UNION
SELECT'ABCXDEYFGH') AS X
ORDER BY X.Tab Collate SQL_Latin1_General_CP850_BIN
I would like to know if the SQL functions extractvalue() and xmltype() have chances to be executed in the following query or if they will be evaluated as a string value by the IN operator.
SELECT * FROM TABLE_1 WHERE (FIELD_1 IN ('foo','bar''||(select extractvalue(xmltype(''%gxgol;]>''),''/l'') from dual)||'''))
This query does not raise any error in Oracle and the output only contains the rows where FIELD_1 equal 'foo'.
No, the content of a string literal will not be executed. Thankfully.
You need a real subquery. Something like:
SELECT *
FROM TABLE_1
WHERE FIELD_1 IN (
SELECT 'foo' FROM DUAL
UNION ALL
SELECT 'bar' || extractvalue(bla,bla) FROM DUAL)
If you call them via
execute immediate
, yes they will taken as functions.
for more info https://docs.oracle.com/cd/B13789_01/appdev.101/b10807/13_elems017.htm
I want to query a table based on the value of a variable. If the variable is null, then I want to return all the rows.I tried using the following query:-
select * from abc_tbl where fld1 like nvl(fld1,'%')
In this table, there are certain rows that have blank values.Those rows are not returned when I used the above query. I tried using regular expressions also-
select * from abc_tbl where regexp_like (fld1,'(.)*')
But none of this is meeting the requirement. Kindly suggest a solution
try this query
select * from abc_tbl where fld1 is null