What is Teradata's "like any" syntax in oracle? - sql

I am converting Teradata syntax to Oracle.
I have this
SELECT * FROM TABLE_A
WHERE proc_name LIKE any ('%AB%','%AC%')
in Teradata but it is not supported in Oracle.
Does anyone know what is the alternative syntax of Like any in Oracle?
Thanks a lot!

Use or:
SELECT *
FROM TABLE_A
WHERE proc_name LIKE '%AB%' or
proc_name LIKE '%AC%';
This is the "normal" way the logic would be expressed in SQL.
Oracle also supports regular expressions, so if you prefer:
SELECT *
FROM TABLE_A
WHERE regexp_like(proc_name, 'AB|AC');
The two likes probably have better performance.

Related

Like operator condition in postgresql data issue

I have below data in the table
HJ-DEF-ABCF010-ABC18-09-17-D
GHJ-ABC-ABFV006-ABC18-09-18-R
OH-DEF-ABFCRT2037-ABC17-01-18-R
I want to populate the value in another column like
HJ-DEF-ABCF010-ABC18-09-17-D BET
GHJ-ABC-ABFV006-ABD18-09-18-R BET
OH-DEF-ABFCRT2037-ABCD17-01-18-R BET
As the mapping for
ABC18 is BET
ABD18 is BET
ABCD17 is BET
I was using the below sql query for that
select col1,case
when col1 like '%-ABC[1-2][0-9]-%' then BET
when col1 like '%-ABD[1-2][0-9]-%' then BET
when col1 like '%-ABCD[1-2][0-9]-%' then BET
else - end form table
which is working fine in SQl sever but in Pogresql we can't use [1-2] to find out the expected digit in the position. Any suggestion or idea how to achieve in Posgresql.
It works just fine. However, you think -- for some reason -- that LIKE supports character classes and other regular-expression-like features. That is simply not true in Postgres, nor in any other database other than SQL Server, Sybase, and MS Access.
Just use regular expressions.
For the original version:
SELECT *
FROM Customers
WHERE Customerid ~ '^[1-2][1-1]';
For the edited version:
SELECT *
FROM Customers
WHERE Customerid ~ '[-].{3}[1-2]1';
You can actually make your current logic work with LIKE, with a bit of effort:
SELECT *
FROM Customers
WHERE Customerid LIKE '11%' OR Customerid LIKE '21%';
You seem to be using perhaps SQL Server or Sybase enhanced LIKE syntax. Postgres does not support this, but it does support its own ~ regex operator. See Gordon's answer for more on that.
The edit to your question can use this:
SELECT *
FROM Customers
WHERE Customerid ~ '-[^-]{3}[1-2]7';
Demo
Used with SIMILAR TO instead of like. select col1,case when col1 SIMILAR TO '%-AB([CD]|CD)[1-2][0-9]-%' then BET else - end form table thnx #WiktorStribiżew

Equivalent to minus in netezza

I want to compare data between two different db tables in netezza. In oracle we can do that by minus operator. How can the same operation be done in netezza.
SELECT CUSTOMER_SRC_ID,CUSTOMER_SRC_DESC FROM CIDB_SIT..CUSTOMER_SRC
MINUS
SELECT CUSTOMER_SRC_ID,CUSTOMER_SRC_DESC FROM EDW_SIT..CUSTOMER_SRC
Seems like it doesn't work in netezza. Can any one help me find the equivalent query in netezza?
The ANSI-SQL standard calls this operators except. Netezza implements it, as do PostgreSQL and MS SQL Server:
SELECT CUSTOMER_SRC_ID,CUSTOMER_SRC_DESC FROM CIDB_SIT..CUSTOMER_SRC
EXCEPT -- Here
SELECT CUSTOMER_SRC_ID,CUSTOMER_SRC_DESC FROM EDW_SIT..CUSTOMER_SRC
You could use the EXCEPT
or
--if customer_src_id is unique--
SELECT CUSTOMER_SRC_ID,CUSTOMER_SRC_DESC
FROM CIDB_SIT..CUSTOMER_SRC
WHERE CUSTOMER_SRC_ID NOT IN (SELECT CUSTOMER_SRC_ID FROM EDW_SIT..CUSTOMER_SRC);

Oracle SQL Syntax: With clause

I'm currently using the Java Version of General SQL Parser for Oracle for some relatively complex Oracle SQL Queries.
As in my case I have no access to any Oracle DB but only have the SQL statements in a file I encounter some statements where the parser fails, one particular boils down to following.
select id from (
with foo as (
select bar from sometable
)
select *
from foo
)
The with clause can be parsed without problem, if not nested.
with foo as (
select bar from sometable
)
select *
from foo
So do I have a bug in the parser or in the statement?
Best,
Will
The SQL statement is valid, so I guess the parser just can't handle it.
To be sure, try running the SQL in SQL Plus.
This is a perfectly valid statement in Oracle (I just tried it).
But it might not be valid ANSI SQL and that might be the reason why the parser doesn't understand it.

SQL query where clause is too long

I've written an SQL query with a lot of 'or's in the 'where' clause:
i.e.
"SELECT * FROM myTable WHERE col1='a' or col1='b' or col1='c'...etc"
I'm trying to run a query in access via vb.net, but I keep getting "Query is too complex" error message.
I'm guessing I've hit some maximum limit. Anyone know a way around this, other than just to break it down into multiple queries?
How about using the IN operator instead?
SELECT Field1, Field2
FROM Table1
WHERE Field1 IN('Val1','Val2', ....)
If you query is that simple would you not be better using
SELECT * FROM myTable WHERE col1 in ('a','b','c')
but it would help to post the actual query so we can give a accurate answer
You could use SQL IN operator instead having multiple OR conditions.
You can use IN as in -
SELECT * FROM myTable WHERE col1 IN ('a','b','c','d');
Many have stated that you should use the IN-operator instead, but when that is used together with constants I believe the optimiser simply converts that to an OR-operator.
Instead you could load a temporary table with your constants and then use the IN-operator with that table.

Oracle DB: How can I write query ignoring case?

As I had written in title, I have SQL query, run on Oracle DB, lets say:
SELECT * FROM TABLE WHERE TABLE.NAME Like 'IgNoReCaSe'
If I would like, that the query would return either "IGNORECASE", "ignorecase" or combinations of them, how can this be done?
Select * from table where upper(table.name) like upper('IgNoreCaSe');
Alternatively, substitute lower for upper.
Use ALTER SESSION statements to set comparison to case-insensitive:
alter session set NLS_COMP=LINGUISTIC;
alter session set NLS_SORT=BINARY_CI;
If you're still using version 10gR2, use the below statements. See this FAQ for details.
alter session set NLS_COMP=ANSI;
alter session set NLS_SORT=BINARY_CI;
You can use either lower or upper function on both sides of the where condition
You could also use Regular Expressions:
SELECT * FROM TABLE WHERE REGEXP_LIKE (TABLE.NAME,'IgNoReCaSe','i');
You can use the upper() function in your query, and to increase performance you can use a function-base index
CREATE INDEX upper_index_name ON table(upper(name))
You can convert both values to upper or lowercase using the upper or lower functions:
Select * from table where upper(table.name) like upper('IgNoreCaSe')
or
Select * from table where lower(table.name) like lower('IgNoreCaSe');
In version 12.2 and above, the simplest way to make the query case insensitive is this:
SELECT * FROM TABLE WHERE TABLE.NAME COLLATE BINARY_CI Like 'IgNoReCaSe'
...also do the conversion to upper or lower outside of the query:
tableName:= UPPER(someValue || '%');
...
Select * from table where upper(table.name) like tableName
Also don't forget the obvious, does the data in the tables need to have case? You could only insert rows already in lower case (or convert the existing DB rows to lower case) and be done with it right from the start.