---
title: "How to Use DECODE in SQL Query and Its Real Purpose"
description: "The DECODE scalar function in DB2 SQL helps you handle logic related scenarios. You can also handle NULL values and you can replace NULL values with values as you wish in your end result."
url: https://srinimf.com/2016/10/06/how-to-write-decode-function-effectively-with-little-effort/
date: 2016-10-06
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2016/10/pexels-photo-6853441.jpeg?fit=1880%2C1253&ssl=1
categories: ["SQL Functions"]
tags: ["How to handle NULL with DECODE"]
type: post
lang: en
---

# How to Use DECODE in SQL Query and Its Real Purpose

The DECODE function’s real purpose is to handle logic scenarios. It works similarly to using the if-else-endif of any high-level programming language.

Table of contents

1. [The syntax for the DECODE function](https://srinimf.com/2016/10/06/how-to-write-decode-function-effectively-with-little-effort/#the-syntax-for-the-decode-function)
2. [SQL Query with DECODE](https://srinimf.com/2016/10/06/how-to-write-decode-function-effectively-with-little-effort/#sql-query-with-decode)

## **The syntax for the DECODE function**

```
DECODE(expression1,expression2,result-expression,else-expression).
```

Here, the expression1 is “Column name”, and it compares with the next value. If it matches, replaces it with a third value.

## **[SQL Query](https://srinimf.com/2016/10/01/how-to-write-sql-query-the-best-way-for-pivoting-tables/) with DECODE**

```
DECODE(c1, 7, 'a', 
 6, 'b', 'c')
DECODE(c1, var1, 'a', var2, 'b')

SELECT ID, DECODE(STATUS, 
 'A', 'Accepted',
 'D', 'Denied',
 CAST(NULL AS VARCHAR(1)), 'Unknown', 'Other')
FROM CONTRACTS
```

Related

- [SQL Query You Need to Create Pivoting Table](https://srinimf.com/2016/10/01/how-to-write-sql-query-the-best-way-for-pivoting-tables/)DateOctober 1, 2016
- [How to Create SQL UDF Quickly](https://srinimf.com/2017/10/12/spilt-string-sql-logic-to-create-function/)DateOctober 12, 2017
- [Aggregate functions Vs Scalar functions in DB2](https://srinimf.com/2015/10/14/aggregate-functions-vs-scalar-functions-in-db2/)DateOctober 14, 2015

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2016%2F10%2F06%2Fhow-to-write-decode-function-effectively-with-little-effort%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2016%2F10%2Fpexels-photo-6853441.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=%3Cstrong%3EHow%20to%20Use%20DECODE%20in%20SQL%20Query%20and%20Its%20Real%20Purpose%3C%2Fstrong%3E)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2016/10/06/how-to-write-decode-function-effectively-with-little-effort/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2016/10/06/how-to-write-decode-function-effectively-with-little-effort/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2016/10/06/how-to-write-decode-function-effectively-with-little-effort/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2016/10/06/how-to-write-decode-function-effectively-with-little-effort/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20%3Cstrong%3EHow%20to%20Use%20DECODE%20in%20SQL%20Query%20and%20Its%20Real%20Purpose%3C%2Fstrong%3E&body=https%3A%2F%2Fsrinimf.com%2F2016%2F10%2F06%2Fhow-to-write-decode-function-effectively-with-little-effort%2F&share=email)
-

### Like this:

Like Loading…