Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NVL should NOT be replaced by COALESCE in Oracle but stay NVL. Their behavior are not the same in term of data type conversion. #3733

Closed
Hal-H2Apps opened this issue Jul 3, 2024 · 0 comments · Fixed by #3734
Assignees

Comments

@Hal-H2Apps
Copy link

Hal-H2Apps commented Jul 3, 2024

Fully reproducible code snippet
In oracle DB :

CREATE TABLE TEST1 (COL1 INT, COL2 VARCHAR2(10))
INSERT INTO TEST1 VALUES (1,'2')
SELECT * FROM TEST1 WHERE NVL(COL2, 1) <> COL1

--> COL1 COL2
1 2

SELECT * FROM TEST1 WHERE COALESCE(COL2, 1) <> COL1

--> ORA-00932: inconsistent datatypes: expected : CHAR ; got : NUMBER

As we can see, NVL does NOT care about the type, COALESCE does. Therefore both function are not identical.

In python with sqlglot:
sqlQuery: str = """ SELECT * FROM TEST1 WHERE NVL(COL2, 1) <> COL1"""
sql = sqlglot.parse_one(sql=sqlQuery, read='oracle')
print(sql.sql(dialect='oracle'))
-> SELECT * FROM TEST1 WHERE COALESCE(COL2, 1) <> COL1
I should have NVL and not COALESCE. These 2 functions are not identical.

Official Documentation
Please include links to official SQL documentation related to your issue.

https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/NVL.html
"If expr1 is character data, then Oracle Database converts expr2 to the data type of expr1 before comparing them and returns VARCHAR2 in the character set of expr1."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants