Fix ORA-00932: Expected – Got CLOB

ORA-00932: inconsistent datatypes: expected - got CLOB means Oracle received a CLOB where the SQL operation requires a different datatype; fix the operation or supply a compatible expression. Common triggers include DISTINCT, grouping, sorting, ordinary equality comparisons, and UNION involving a CLOB. Start by identifying the failing expression rather than converting every CLOB to VARCHAR2, which can introduce size errors or change your results.

Use this table to match the failing part of your query to the appropriate repair.

Where the Error Appears Likely Cause Appropriate Fix
SELECT DISTINCT includes a CLOB Duplicate elimination cannot operate directly on the CLOB Deduplicate by scalar keys, or explicitly compare complete CLOB contents
GROUP BY or ORDER BY references a CLOB The operation requires a supported grouping or sorting datatype Group or sort by scalar fields; retrieve the CLOB without grouping or sorting it
A join or filter uses = between CLOBs Ordinary SQL equality does not support this LOB comparison Use scalar join keys and DBMS_LOB.COMPARE for content equality
UNION combines queries returning CLOBs UNION requires duplicate elimination Use UNION ALL if duplicates are acceptable
A function, conditional expression, or application parameter fails An expression or bind type is incompatible with its context Match the expression or bind datatype to what that operation accepts

Why Oracle Rejects a CLOB in a Query

A CLOB is a character large object, intended for character data that may exceed ordinary string limits. Oracle can store and retrieve CLOBs, but it does not support them in every operation that accepts VARCHAR2.

For example, selecting a CLOB column can work while adding DISTINCT makes the query fail. The problem is not necessarily the column definition or its contents: it is the operation applied to that datatype.

A short value stored in a CLOB column is still a CLOB. Having only a few characters in the failing row does not make an unsupported operation valid.

Also check expressions that produce a CLOB. A view, function, or XML aggregation ending in getclobval() can return one even when the underlying text columns are ordinary strings.

In the older error wording, the dash in expected - does not identify a useful target datatype. You need the failing SQL operation, or more detailed error output, to determine what Oracle requires.

How to Fix the CLOB Datatype Error

The examples below assume these illustrative objects:

  • documents: a unique, non-null document_id, plus a CLOB column named body.
  • document_tags: document_id and tag, potentially containing multiple matching rows per document.
  • document_archive: document_id and another CLOB body.

Replace those names with your schema’s objects.

Step 1: Identify the Failing Expression and Its Actual Type

  1. Capture the complete database error, the SQL sent to Oracle, and any bind parameter datatypes. For application-generated queries, inspect the executed SQL rather than only the query-builder configuration.
  2. When detailed error output is available, read these fields: – failed_expression: the expression with the incompatible datatype. – expression_data_type: the actual datatype of that column or expression. – expected_data_type: the datatype required by the SQL operation. These distinctions matter because the repair must satisfy the operation’s requirements, according to Oracle’s ORA-00932 error documentation.
  3. Inspect the table or view definition. For an object you own, this query lists its declared column types:
    SELECT table_name, column_name, data_type
    FROM user_tab_columns
    WHERE table_name = 'DOCUMENTS'
    ORDER BY column_id;

    Oracle normally stores unquoted object names in uppercase. If the object belongs to another schema, inspect its definition through metadata accessible to your account.

Do not stop at the base column’s type. A selected function result, a view column, or an application bind can be the incompatible expression.

Step 2: Isolate the Operation That Introduces the Error

  1. Start with the simplest projection that retrieves the relevant CLOB:
    SELECT document_id, body
    FROM documents;
  2. Reintroduce the original query’s joins, filters, selected expressions, grouping, set operations, and sorting incrementally. Run the query after each change.
  3. Inspect every query layer. An inner query may execute successfully while an outer query applies DISTINCT to its CLOB result. A reporting tool can add that outer operation automatically.

If the plain projection succeeds, the CLOB can be retrieved. Focus on the newly added operation rather than changing the stored data.

Removing a clause is a diagnostic test, not automatically a finished repair. For example, removing DISTINCT might eliminate the error while introducing duplicate business records.

Step 3: Rewrite the Operation Without Losing the Required Data

Choose the rewrite that preserves your intended results.

  1. Use EXISTS when a join is only checking for a matching row. A common failing query joins documents to tags and then uses DISTINCT to remove duplicate documents:
    SELECT DISTINCT d.document_id, d.body
    FROM documents d
    JOIN document_tags t
      ON t.document_id = d.document_id
    WHERE t.tag = :tag;

    If the requirement is “return each document that has this tag,” use an existence test:

    SELECT d.document_id, d.body
    FROM documents d
    WHERE EXISTS (
        SELECT 1
        FROM document_tags t
        WHERE t.document_id = d.document_id
          AND t.tag = :tag
    );

    With a unique document_id in documents, this returns each matching document once while preserving its complete CLOB. It does not require Oracle to deduplicate the text.

  2. Aggregate scalar fields before retrieving the CLOB. If you need a tag count per document, calculate that count separately instead of grouping by the document’s CLOB:
    WITH tag_counts AS (
        SELECT document_id, COUNT(*) AS tag_count
        FROM document_tags
        GROUP BY document_id
    )
    SELECT d.document_id, d.body, c.tag_count
    FROM documents d
    JOIN tag_counts c
      ON c.document_id = d.document_id
    ORDER BY d.document_id;

    This returns documents with tag rows, their complete bodies, and the count of tag rows per document. Use a left join and an appropriate null-to-zero expression if documents without tags must also appear. The sorting field must also match the business requirement. Ordering by an ID is not equivalent to alphabetically ordering complete document text.

  3. Use DBMS_LOB.COMPARE when equality really means matching CLOB contents. Match candidate records through scalar keys, then compare their CLOBs:
    SELECT d.document_id
    FROM documents d
    JOIN document_archive a
      ON a.document_id = d.document_id
    WHERE DBMS_LOB.COMPARE(d.body, a.body) = 0
       OR (d.body IS NULL AND a.body IS NULL);

    DBMS_LOB.COMPARE returns zero for matching content and a nonzero result for different content. The additional condition explicitly treats two null bodies as equal; remove it if that is not your intended rule. Restrict candidates using scalar keys where possible. Comparing large text values across many unrelated row pairs can be expensive.

Step 4: Test the Rewritten Query in Its Original Context

  1. Run the complete rewritten SQL with representative bind values.
  2. Check row identities, row counts, and complete text, not just whether the exception disappeared.
  3. Run the original application or report action again. Confirm that it does not add an incompatible outer query.
  4. Inspect the execution plan if the rewrite introduces LOB comparisons or changes joins.

For the examples above, verify that a document with multiple matching tags appears once, that tag counts are correct, and that equal and unequal document bodies produce the intended comparison results. Only then replace the original query.

When to Convert a CLOB to VARCHAR2

Converting a CLOB is not a universal fix: it can replace a datatype error with a buffer-size error.

A conversion can fail with ORA-64203: Destination buffer too small to hold CLOB data after character set conversion. The destination must accommodate the converted text, not merely the number of characters you expect to see.

Use a Substring for an Intentional Preview

For a display preview, explicitly extract a bounded prefix:

SELECT document_id,
       DBMS_LOB.SUBSTR(body, 1000, 1) AS body_preview
FROM documents;

For a CLOB, the second argument requests characters, and the third is the starting character position. This example requests a 1,000-character preview beginning at the first character; that is a chosen display length, not a universal conversion limit.

The result is a preview, not the complete CLOB. Two documents can share this prefix and differ later, so using the preview for equality, grouping, or duplicate elimination changes the meaning of those operations.

Test the preview with your longest and multibyte values, and confirm that the application intentionally displays only that prefix. Keep the original CLOB available wherever users need the full text.

Convert Complete Values Only When They Fit

In Oracle Database SQL, VARCHAR2 has a maximum size of 4,000 bytes with MAX_STRING_SIZE=STANDARD, or 32,767 bytes with MAX_STRING_SIZE=EXTENDED. These are byte limits; multibyte text can reach them with fewer characters.

PL/SQL variables, SQL expressions, driver buffers, and destination columns can impose different constraints. A conversion that works in one context is not proof that it works in another.

Use a full conversion only when your application enforces a text-size bound that fits the destination. Test that boundary, including multibyte text, and reject oversized values explicitly rather than silently replacing full text with a prefix.

Do not change MAX_STRING_SIZE merely to suppress this error. A larger scalar-string limit neither makes CLOBs valid in unsupported operations nor guarantees that an arbitrary CLOB will fit.

Special Cases With CLOB Results

UNION ALL, CLOB-compatible conditional branches, complete-content comparisons, and generated SQL checks address CLOB results without relying on unsupported duplicate elimination or sorting.

UNION and Other Set Operations

When you need to append rows rather than remove duplicates, UNION ALL can preserve CLOB columns:

SELECT document_id, body
FROM documents
UNION ALL
SELECT document_id, body
FROM document_archive;

Both corresponding body expressions here are CLOBs, and the ID expressions must also be compatible.

Run the combined query and confirm that keeping duplicate rows is acceptable. Changing UNION to UNION ALL is not equivalent if the original requirement includes duplicate elimination.

Converting both branches to CLOB does not make UNION itself support CLOB duplicate elimination.

Conditional Expressions and Function Arguments

If the error points to CASE, NVL, COALESCE, or a function call, inspect both the expression’s result type and the surrounding operation. Mixed inputs are not always invalid, some functions support implicit conversion, but you should not rely on conversion where the function signature or SQL context requires something else.

When a conditional result should remain a CLOB, make a literal branch explicitly CLOB-compatible:

SELECT document_id,
       CASE
           WHEN body IS NULL THEN TO_CLOB('No content')
           ELSE body
       END AS body
FROM documents;

Test both null and non-null input rows. This controls the conditional result’s datatype; it does not make that result suitable for an outer DISTINCT or CLOB sort.

For stored functions and procedures, check the declared parameter and return types. If an application supplies the value, check its bind type as well.

Exact Deduplication by Complete CLOB Content

Deduplicating documents by ID is different from deduplicating their text. If the requirement is to retain one row per complete CLOB value, a prefix conversion is not sufficient.

For a small dataset with a unique, non-null document_id, this query retains the lowest-ID row for each matching body:

SELECT d.document_id, d.body
FROM documents d
WHERE NOT EXISTS (
    SELECT 1
    FROM documents earlier
    WHERE earlier.document_id < d.document_id
      AND (
          DBMS_LOB.COMPARE(earlier.body, d.body) = 0
          OR (earlier.body IS NULL AND d.body IS NULL)
      )
);

This explicitly treats null bodies as duplicates of other null bodies. It compares stored content without applying case folding or whitespace normalization.

Test it with identical bodies, different bodies sharing a long prefix, and nulls. Confirm that the retained ID is the lowest one for each group.

The comparison workload can grow rapidly as the table grows. For large-scale deduplication, use a design that narrows candidate matches before comparing complete content; do not treat a matching hash alone as proof of equality.

Reporting Tools, ORMs, and Views

A report can retrieve a CLOB successfully until a “hide duplicates,” grouping, or sorting option changes the generated SQL. An ORM can similarly add DISTINCT when fetching related entities.

Capture the final SQL and determine whether duplicate handling applies to the business entity or the text itself. If you need one row per entity, correct the join or deduplicate scalar identifiers before retrieving the CLOB.

Views can hide the datatype change. XML aggregation followed by getclobval(), for example, produces a CLOB that a later query may try to compare or deduplicate.

After changing the query or report configuration, rerun the original user action and inspect the generated SQL again. Turning off duplicate suppression is only complete when the resulting duplicates are either acceptable or prevented by the query design.

How to Verify and Prevent CLOB Type Errors

A successful execution is necessary, but verify that the rewrite preserved your data.

Verify result meaning, text completeness, edge cases, application behavior, and performance:

  • Result meaning: Each required business record appears with the intended frequency.
  • Text completeness: Full-text operations still use complete CLOB values, not previews.
  • Edge cases: Tests include nulls, empty CLOBs, multibyte text, and long values.
  • Late differences: Two bodies that differ only near the end are still distinguished.
  • Application behavior: The original report, endpoint, or job executes without adding an incompatible operation.
  • Performance: LOB comparisons run against a suitably restricted candidate set.

An empty CLOB and a null CLOB are different states, so define how your application should treat each rather than assuming they are interchangeable.

To prevent recurrence, keep identifiers, grouping fields, and ordinary sort keys in suitable scalar columns. Make preview fields visibly distinct from full-content fields, and add regression tests around generated SQL that handles large text.

Once the repaired query passes these checks, save a minimal failing example and its expected results alongside the regression test.

Frequently Asked Questions

What Does an ORA Error Mean in Oracle?

ORA- identifies an Oracle Database error message; the number and accompanying text identify the particular failure. For an inconsistent-datatype error, investigate the expression and operation named in the message.

Does “Expected CHAR Got LONG” Have the Same Fix?

No. It belongs to the same datatype-error family, but LONG is a legacy datatype with different restrictions from CLOB. Do not apply DBMS_LOB functions directly to a LONG; inspect the failing operation and use a supported extraction or migration approach for that datatype.

Why Does PL/SQL Report “Expected PLSQL INDEX TABLE Got NUMBER”?

“Expected PLSQL INDEX TABLE Got NUMBER” means an operation expected a PL/SQL collection but received a numeric scalar. Check the procedure or function signature, collection declarations, and application bind types. Converting a CLOB or changing duplicate elimination will not resolve that mismatch.