{
  "id": "redis-sql-case-example",
  "title": "Using SQL CASE",
  "url": "https://redis.io/docs/latest/integrate/redis-data-integration/data-pipelines/transform-examples/redis-sql-case-example/",
  "summary": "",
  "tags": [
    "docs",
    "integrate",
    "rs",
    "rdi"
  ],
  "last_updated": "2026-04-01T08:10:08-05:00",
  "page_type": "content",
  "content_hash": "448a139a118b6cd6f182c54f976ef1285d5ea722da1301df753c52268239e3e0",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "The [`CASE`](https://www.w3schools.com/sql/sql_case.asp) statement allows you to specify conditions and return different values based on those conditions. You can use it both to create new fields or filter existing data."
    },
    {
      "id": "using-sql-case-to-create-a-new-field",
      "title": "Using SQL CASE to create a new field",
      "role": "content",
      "text": "The example below demonstrates how to use the `CASE` statement to create a new field called `Market` based on the value of the `BillingCountry` field in the `Invoice` table. The new field categorizes countries into regions such as \"North America\" and \"Europe\".\n\n[code example]"
    },
    {
      "id": "using-sql-case-to-filter-data",
      "title": "Using SQL CASE to filter data",
      "role": "content",
      "text": "You can also use the `CASE` statement to filter data based on specific conditions. The example below demonstrates how to filter the `Invoice` table to include only invoices from the USA and Canada that have a `Total` value above their country-specific threshold.\n\nBecause the `Total` field is a Decimal in the source table, it is represented as a string in Debezium and so you must cast it to `REAL` to compare it numerically in the `CASE` statement. Without this cast, it will be compared as a string value, which will give the wrong result.\n\n[code example]"
    }
  ],
  "examples": [
    {
      "id": "using-sql-case-to-create-a-new-field-ex0",
      "language": "yaml",
      "code": "name: Add market field using SQL CASE\nsource:\n  table: Invoice\n\ntransform:\n  - uses: add_field\n    with:\n      field: \"Market\"\n      language: sql\n      expression: |\n        CASE\n          WHEN BillingCountry = 'USA' THEN 'North America'\n          WHEN BillingCountry = 'Canada' THEN 'North America'\n          WHEN BillingCountry = 'UK' THEN 'Europe'\n          WHEN BillingCountry = 'France' THEN 'Europe'\n          ELSE 'Other'\n        END",
      "section_id": "using-sql-case-to-create-a-new-field"
    },
    {
      "id": "using-sql-case-to-filter-data-ex0",
      "language": "yaml",
      "code": "name: Filter invoices by country and total\nsource:\n  table: Invoice\n\ntransform:\n  - uses: filter\n    with:\n      language: sql\n      expression: |\n        CASE\n          WHEN BillingCountry = 'USA' AND CAST(Total AS REAL) > 11.99 THEN True\n          WHEN BillingCountry = 'Canada' AND CAST(Total AS REAL) > 9.99 THEN True\n          ELSE False\n        END",
      "section_id": "using-sql-case-to-filter-data"
    }
  ]
}
