ORA-00932 Inconsistent Datatypes - Complete Resolution Guide
ORA-00932: Inconsistent Datatypes
Section titled “ORA-00932: Inconsistent Datatypes”Error Overview
Section titled “Error Overview”Error Text: ORA-00932: inconsistent datatypes: expected string got string
The ORA-00932 error occurs when Oracle encounters a datatype mismatch during SQL execution. This happens when you attempt to compare, assign, or operate on values with incompatible datatypes. This is one of the most common development errors in Oracle databases.
Common Causes
Section titled “Common Causes”1. Comparing Incompatible Types
Section titled “1. Comparing Incompatible Types”- Comparing VARCHAR2 to NUMBER without conversion
- Comparing DATE to VARCHAR2
- Using CLOB/BLOB in equality comparisons
2. INSERT/UPDATE Type Mismatches
Section titled “2. INSERT/UPDATE Type Mismatches”- Inserting string values into NUMBER columns
- Date format mismatches
- LOB handling errors
3. Function Return Type Issues
Section titled “3. Function Return Type Issues”- DECODE/CASE returning mixed datatypes
- NVL with incompatible argument types
- Aggregate functions with wrong column types
4. UNION/UNION ALL Mismatches
Section titled “4. UNION/UNION ALL Mismatches”- Column datatype differences between SELECT statements
- Different number precision/scale
- Character vs numeric columns in same position
Common Scenarios and Solutions
Section titled “Common Scenarios and Solutions”Scenario 1: CLOB Comparison Error
Section titled “Scenario 1: CLOB Comparison Error”-- ERROR: Comparing CLOB directlySELECT * FROM documentsWHERE clob_column = 'search text';-- ORA-00932: inconsistent datatypes: expected - got CLOB
-- SOLUTION: Use DBMS_LOB.INSTR or TO_CHAR (for small CLOBs)SELECT * FROM documentsWHERE DBMS_LOB.INSTR(clob_column, 'search text') > 0;
-- Or for exact match on small CLOBs (< 4000 chars)SELECT * FROM documentsWHERE DBMS_LOB.SUBSTR(clob_column, 4000, 1) = 'search text';Scenario 2: DECODE/CASE Mixed Types
Section titled “Scenario 2: DECODE/CASE Mixed Types”-- ERROR: Mixed types in DECODESELECT DECODE(status, 'A', 'Active', 'I', 0, -- NUMBER mixed with VARCHAR2 'Unknown')FROM users;-- ORA-00932: inconsistent datatypes: expected CHAR got NUMBER
-- SOLUTION: Ensure consistent return typesSELECT DECODE(status, 'A', 'Active', 'I', '0', -- Convert to string 'Unknown')FROM users;
-- Or use CASE with explicit castingSELECT CASE status WHEN 'A' THEN 'Active' WHEN 'I' THEN TO_CHAR(0) ELSE 'Unknown' ENDFROM users;Scenario 3: NVL Type Mismatch
Section titled “Scenario 3: NVL Type Mismatch”-- ERROR: NVL with different typesSELECT NVL(commission_pct, 'N/A') FROM employees;-- ORA-00932: inconsistent datatypes: expected NUMBER got CHAR
-- SOLUTION: Convert to common typeSELECT NVL(TO_CHAR(commission_pct), 'N/A') FROM employees;
-- Or use numeric placeholderSELECT NVL(commission_pct, 0) FROM employees;Scenario 4: UNION Column Mismatch
Section titled “Scenario 4: UNION Column Mismatch”-- ERROR: Different column types in UNIONSELECT employee_id, hire_date FROM employeesUNIONSELECT department_id, department_name FROM departments;-- ORA-00932: inconsistent datatypes: expected DATE got CHAR
-- SOLUTION: Align datatypes with conversionSELECT employee_id, TO_CHAR(hire_date, 'YYYY-MM-DD') FROM employeesUNIONSELECT department_id, department_name FROM departments;Scenario 5: Implicit Conversion Failure
Section titled “Scenario 5: Implicit Conversion Failure”-- ERROR: String to number conversion failsSELECT * FROM ordersWHERE order_id = '12345ABC'; -- order_id is NUMBER-- ORA-00932 or ORA-01722 depending on context
-- SOLUTION: Use proper datatypeSELECT * FROM ordersWHERE order_id = 12345;
-- Or if column should be VARCHAR2, fix the comparisonSELECT * FROM ordersWHERE TO_CHAR(order_id) = '12345ABC';Diagnostic Queries
Section titled “Diagnostic Queries”Identify Column Datatypes
Section titled “Identify Column Datatypes”-- Check table column datatypesSELECT column_name, data_type, data_length, data_precision, data_scaleFROM all_tab_columnsWHERE owner = 'SCHEMA_NAME' AND table_name = 'TABLE_NAME'ORDER BY column_id;
-- Compare columns between tables for UNION operationsSELECT a.column_name, a.data_type as table1_type, b.data_type as table2_type, CASE WHEN a.data_type != b.data_type THEN 'MISMATCH' ELSE 'OK' END as statusFROM all_tab_columns aJOIN all_tab_columns b ON a.column_id = b.column_idWHERE a.owner = 'SCHEMA' AND a.table_name = 'TABLE1' AND b.owner = 'SCHEMA' AND b.table_name = 'TABLE2';Find LOB Columns
Section titled “Find LOB Columns”-- Identify LOB columns that might cause issuesSELECT owner, table_name, column_name, data_typeFROM all_tab_columnsWHERE data_type IN ('CLOB', 'BLOB', 'NCLOB', 'LONG') AND owner = 'SCHEMA_NAME'ORDER BY table_name, column_name;Check View Definitions for Type Issues
Section titled “Check View Definitions for Type Issues”-- Review view column typesSELECT column_name, data_type, data_type_ownerFROM all_tab_columnsWHERE owner = 'SCHEMA_NAME' AND table_name = 'VIEW_NAME';Resolution Steps
Section titled “Resolution Steps”1. Explicit Type Conversion
Section titled “1. Explicit Type Conversion”-- String to NumberTO_NUMBER('123')TO_NUMBER('1,234.56', '9,999.99')
-- Number to StringTO_CHAR(12345)TO_CHAR(1234.56, '9999.99')
-- String to DateTO_DATE('2024-01-15', 'YYYY-MM-DD')TO_TIMESTAMP('2024-01-15 10:30:00', 'YYYY-MM-DD HH24:MI:SS')
-- Date to StringTO_CHAR(SYSDATE, 'YYYY-MM-DD')TO_CHAR(SYSTIMESTAMP, 'YYYY-MM-DD HH24:MI:SS.FF')2. Handle LOB Comparisons
Section titled “2. Handle LOB Comparisons”-- For CLOB searchingSELECT * FROM documentsWHERE DBMS_LOB.INSTR(content_clob, 'search term', 1, 1) > 0;
-- For CLOB comparison (exact match)SELECT * FROM documentsWHERE DBMS_LOB.COMPARE(content_clob, TO_CLOB('exact text')) = 0;
-- For BLOB comparisonSELECT * FROM filesWHERE DBMS_LOB.COMPARE(file_blob, other_blob) = 0;
-- Convert small CLOB to VARCHAR2SELECT * FROM documentsWHERE TO_CHAR(content_clob) LIKE '%pattern%'; -- Only for < 4000 chars3. Fix DECODE/CASE Statements
Section titled “3. Fix DECODE/CASE Statements”-- Ensure all branches return same typeSELECT CASE WHEN status = 1 THEN 'Active' WHEN status = 2 THEN 'Inactive' WHEN status = 3 THEN 'Pending' ELSE 'Unknown' -- All return VARCHAR2 END as status_descFROM accounts;
-- Use CAST for explicit typingSELECT CASE WHEN flag = 'Y' THEN CAST(1 AS NUMBER) ELSE CAST(0 AS NUMBER) END as flag_numFROM settings;4. Fix UNION Queries
Section titled “4. Fix UNION Queries”-- Align all columns to compatible typesSELECT TO_CHAR(id) as id, name, TO_CHAR(created_date, 'YYYY-MM-DD') as date_strFROM table1UNION ALLSELECT code as id, description as name, effective_date as date_strFROM table2;Prevention Strategies
Section titled “Prevention Strategies”1. Use Explicit Conversions
Section titled “1. Use Explicit Conversions”-- Always be explicit about type conversions-- Instead of relying on implicit conversion:WHERE order_date = '2024-01-15' -- Implicit
-- Use explicit conversion:WHERE order_date = TO_DATE('2024-01-15', 'YYYY-MM-DD') -- Explicit2. Validate Input Data
Section titled “2. Validate Input Data”-- Create validation functionCREATE OR REPLACE FUNCTION is_number(p_str VARCHAR2) RETURN NUMBER IS v_num NUMBER;BEGIN v_num := TO_NUMBER(p_str); RETURN 1;EXCEPTION WHEN VALUE_ERROR THEN RETURN 0;END;/
-- Use in queriesSELECT * FROM dataWHERE is_number(string_column) = 1 AND TO_NUMBER(string_column) > 100;3. Consistent Column Types in Design
Section titled “3. Consistent Column Types in Design”-- Use consistent types for similar data-- Define standards:-- IDs: NUMBER(12) or VARCHAR2(36) for UUIDs-- Dates: DATE or TIMESTAMP-- Flags: VARCHAR2(1) with 'Y'/'N' or NUMBER(1) with 1/0-- Money: NUMBER(19,4)4. Code Review Checklist
Section titled “4. Code Review Checklist”- Check all DECODE/CASE return types match
- Verify UNION/UNION ALL column alignment
- Review NVL/COALESCE argument types
- Validate LOB column operations
- Test with edge case data
Common Error Variations
Section titled “Common Error Variations”| Error Message | Cause | Solution |
|---|---|---|
| expected NUMBER got CHAR | String compared to number | Use TO_NUMBER() |
| expected DATE got CHAR | String compared to date | Use TO_DATE() |
| expected - got CLOB | Direct CLOB comparison | Use DBMS_LOB functions |
| expected CHAR got NUMBER | Number in string context | Use TO_CHAR() |
| expected CHAR got CLOB | CLOB in VARCHAR2 context | Use TO_CHAR() or DBMS_LOB |
Related Errors
Section titled “Related Errors”- ORA-01722 - Invalid number (conversion failure)
- ORA-01858 - Non-numeric character in date
- ORA-01861 - Literal does not match format string
- ORA-06502 - PL/SQL numeric or value error
Best Practices Summary
Section titled “Best Practices Summary”- Always use explicit type conversion functions (TO_CHAR, TO_NUMBER, TO_DATE)
- Match datatypes in conditional expressions (DECODE, CASE, NVL)
- Handle LOB columns specially - use DBMS_LOB package
- Verify UNION column alignment before combining queries
- Test with boundary data including NULLs and edge cases
- Document expected datatypes in stored procedures and functions