{
  "schema_version": 2,
  "id": "integrate/redis-data-integration/data-pipelines/transform-examples/redis-sql-case-example",
  "title": "Using SQL CASE",
  "url": "https://redis.io/docs/latest/integrate/redis-data-integration/1.19.1/data-pipelines/transform-examples/redis-sql-case-example/",
  "summary": "",
  "content": "\nThe [`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.\n\n## Using SQL CASE to create a new field\nThe 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```yaml\nname: 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\n```\n\n## Using SQL CASE to filter data\n\nYou 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```yaml\nname: 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) \u003e 11.99 THEN True\n          WHEN BillingCountry = 'Canada' AND CAST(Total AS REAL) \u003e 9.99 THEN True\n          ELSE False\n        END\n```\n",
  "tags": ["docs","integrate","rs","rdi"],
  "last_updated": "2026-09-19T17:55:58-07:00"
}
