# CASE expression returns mixed types: conversion failed / types cannot be matched

**Code:** `CASE` · **Category:** SQL · **Seen on:** SQL, PostgreSQL, SQL Server, Oracle

The branches of a CASE expression produce incompatible data types (text and number, date and string).

## Meaning

All THEN/ELSE results must resolve to a common type. Cast branches explicitly (`CAST(x AS varchar)`), and watch for `NULL`/empty strings mixed with numbers.

## Common causes

- Number and text mixed in the branches
- Date and string branches
- Implicit conversion order (data type precedence)
- Empty string in a numeric branch
- NULL typed unexpectedly

## Quick fix

1. Cast every branch to the same type
2. Use `NULL` or `0` consistently
3. Check data type precedence rules for your DB

## Detailed fix by platform

### SQL

1. `SELECT CASE WHEN qty > 0 THEN CAST(qty AS varchar(10)) ELSE 'none' END FROM stock;`

## How to diagnose

1. **Branch types** — Same?
2. **Cast** — Explicit?
3. **Empty string** — In numeric branch?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/sql/case-expression-result-type-mismatch)
