Oracle's Empty String to NULL Behavior and SQL Portability
Oracle treats empty strings as NULL, impacting SQL portability. Learn how to write portable SQL by using IS NULL.
On this page
The short answer
Oracle Database treats a character value with a length of zero (an empty string) as NULL. This behavior is a deviation from the SQL standard, which generally distinguishes between an empty string and a NULL value. In Oracle, when you insert an empty string into a VARCHAR2 column, it is stored and treated as NULL. This means that operations and comparisons involving empty strings in Oracle will behave as if they involve NULL values. This can lead to significant issues when migrating SQL code to or from other database systems that adhere more strictly to the SQL standard or have different default behaviors for empty strings. For instance, in SQL Server, with SET ANSI_NULLS ON (the default and standard behavior), comparing a column to an empty string (column = '') will not yield rows where the column is NULL. Conversely, in Oracle, column = '' might return rows where the column is NULL because Oracle treats '' as NULL. The standard and correct way to check for the absence of a value, whether it's explicitly NULL or an empty string treated as NULL by the database, is to use the IS NULL operator. Therefore, WHERE my_column IS NULL is the portable and universally correct way to test if a column contains no value, regardless of whether the database system interprets empty strings as NULL or treats them distinctly.
Oracle's Treatment of Empty Strings
Oracle Database has a specific behavior where it interprets a character value with a length of zero, commonly known as an empty string, as a NULL value. This applies to columns of character data types, such as VARCHAR2, provided they are not constrained by NOT NULL or PRIMARY KEY integrity constraints. When an empty string is inserted into such a column in Oracle, it is stored and subsequently treated as NULL.
Oracle explicitly recommends against treating empty strings the same as NULLs for future compatibility. The documentation notes that while the current behavior treats zero-length character values as null, this may not continue to be true in future releases. Consequently, any arithmetic expression involving a value treated as NULL, including an empty string, will result in NULL.
Practical tip
Always use IS NULL and IS NOT NULL for checking the presence or absence of values in columns, rather than comparing with empty strings or specific literal values, to ensure maximum SQL code portability across different database systems.
SQL Standard vs. Oracle's Empty String Handling
The SQL standard generally distinguishes between a NULL value, which represents an unknown or missing value, and an empty string, which is a string of zero length. Many database systems, such as SQL Server (when SET ANSI_NULLS is ON) and PostgreSQL, adhere to this distinction. In these systems, an empty string is treated as a distinct value, not as NULL. Consequently, comparisons with empty strings behave differently from comparisons with NULL.
Oracle's deviation from this standard means that an empty string literal '' in a query against an Oracle database will often be evaluated as if it were NULL. This can be a source of confusion and errors when developers are accustomed to the behavior of other SQL databases or the SQL standard itself. The SQL standard implies that '' is a valid, albeit empty, string, whereas NULL signifies the absence of any value.
Impact on SQL Code Portability
The primary impact of Oracle's empty string handling on SQL code portability arises when SQL statements are written without considering this specific behavior. If a query relies on the distinction between empty strings and NULLs, it may function correctly in Oracle but fail or produce unexpected results in other database systems, and vice versa. For example, a query designed to find records with no value in a certain column might use WHERE my_column = '' in Oracle, expecting to retrieve rows where the column is NULL. However, if this same query is run on SQL Server with default settings, it will not return rows where my_column is NULL, as '' is not treated as NULL.
Migrating code from Oracle to another database might require extensive modifications to replace all instances of column = '' with column IS NULL or to handle empty strings as distinct values if that is the intended logic. Conversely, migrating code from another database to Oracle might introduce bugs if the original code implicitly relied on '' being different from NULL.
Comparison Operators and NULLs
In SQL, comparison operators like =, <, >, <=, >=, <> (or !=) have a specific behavior when one or both operands are NULL. This behavior is governed by SQL's three-valued logic (TRUE, FALSE, UNKNOWN). When comparing any value (including another NULL) to a NULL using standard comparison operators, the result is typically UNKNOWN, not TRUE or FALSE.
For instance, in Oracle, the condition my_column = NULL will always evaluate to UNKNOWN, regardless of the value in my_column. Similarly, my_column != NULL also evaluates to UNKNOWN. The Oracle documentation clarifies: "Because null represents a lack of data, a null cannot be equal or unequal to any value or to another null." This is why standard comparison operators are unsuitable for checking for NULL values. The only exceptions might be specific functions like Oracle's DECODE or NVL which are designed to handle NULLs, or when SET ANSI_NULLS is OFF in SQL Server, which alters the behavior for = and <> operators to treat NULLs as known values.
The UNKNOWN Result
The UNKNOWN result in SQL's three-valued logic is crucial for understanding how conditions involving NULLs are evaluated. When a condition evaluates to UNKNOWN, it is treated similarly to FALSE in many contexts, most notably in the WHERE clause of a SELECT statement. If a condition in the WHERE clause evaluates to UNKNOWN for a particular row, that row will not be included in the result set.
The Oracle documentation explains: "A condition that evaluates to UNKNOWN acts almost like FALSE. For example, a SELECT statement with a condition in the WHERE clause that evaluates to UNKNOWN returns no rows." This is because the WHERE clause filters rows based on conditions that evaluate to TRUE. Conditions evaluating to UNKNOWN or FALSE are excluded. Furthermore, logical operations involving UNKNOWN propagate the uncertainty: NOT UNKNOWN evaluates to UNKNOWN, unlike NOT FALSE which evaluates to TRUE.
Correct NULL Testing: IS NULL and IS NOT NULL
To correctly and portably test for the presence or absence of a value in SQL, the standard operators IS NULL and IS NOT NULL must be used. These operators are specifically designed to handle NULL values and are part of the SQL standard. They provide a clear and unambiguous way to check if a column contains a NULL value.
The condition my_column IS NULL evaluates to TRUE if my_column contains a NULL value, and FALSE otherwise. Conversely, my_column IS NOT NULL evaluates to TRUE if my_column contains any value (including an empty string if the database treats it as distinct from NULL), and FALSE if it is NULL. Using these operators ensures that your SQL code behaves consistently across different database systems, including Oracle, SQL Server, PostgreSQL, and others, regardless of their specific handling of empty strings. The Oracle documentation emphasizes this: "To test for nulls, use only the comparison conditions IS NULL and IS NOT NULL."
Cross-Database NULL Handling Differences
The primary difference in NULL handling across major SQL databases, particularly concerning empty strings, lies in whether an empty string is treated as a distinct value or as equivalent to NULL. Oracle, by default, treats empty strings as NULL. SQL Server, with SET ANSI_NULLS ON, treats empty strings as distinct from NULL. PostgreSQL also distinguishes between NULL and empty strings. MySQL's behavior can depend on the SQL mode, but typically it distinguishes between NULL and empty strings.
This divergence means that queries like WHERE column = '' will yield different results on Oracle compared to SQL Server or PostgreSQL when the column contains NULL values. This inconsistency is a significant pitfall for developers aiming for cross-database compatibility. The universal approach to check for the absence of data is WHERE column IS NULL.
Recommendations for Portable SQL
To write SQL code that is portable and robust across different database systems, developers should adhere to the following recommendations regarding NULL and empty string handling:
1. Always use IS NULL and IS NOT NULL to test for the presence or absence of values. Avoid using comparison operators like = or <> with NULL literals or empty strings when you intend to check for NULL values. For example, use WHERE my_column IS NULL instead of WHERE my_column = '' or WHERE my_column = NULL.
2. Be aware of database-specific behaviors. If you must interact with empty strings as distinct values, ensure your code accounts for the specific database's interpretation. For instance, if you need to find rows with *either* a NULL value *or* an empty string, you might need a condition like WHERE my_column IS NULL OR my_column = '' (though this specific syntax's portability can vary).
3. Explicitly handle NULLs in application logic. When retrieving data, check for NULLs and decide how your application should interpret or display them, rather than relying on implicit database behaviors.
4. Consider using database functions that normalize values if necessary, such as COALESCE(my_column, '') to treat NULLs as empty strings for specific operations, but be mindful that this also impacts portability if the target database doesn't support COALESCE or handles it differently.
Things to check
- Oracle treats empty strings as NULL.
- Standard SQL distinguishes between empty strings and NULL.
- Oracle's behavior can cause SQL code portability issues.
- Comparison operators with NULL result in UNKNOWN.
- WHERE clauses filter out rows where conditions are UNKNOWN.
- IS NULL and IS NOT NULL are the standard for NULL testing.
- Using IS NULL/IS NOT NULL ensures cross-database compatibility.
- Empty string handling differs across major SQL databases.
Where this applies
This guide focuses on the general behavior of Oracle, SQL Server, and the SQL standard regarding empty strings and NULLs. Specific versions or configurations of database systems might exhibit minor variations. It does not cover all possible edge cases or the nuances of NULL handling in all SQL dialects.