---
title: "How to Resolve -305 SQL Error in DB2 Quickly"
description: "SQL -305 error code and quick resolution to handle it in your programs"
url: https://srinimf.com/2016/10/30/the-best-way-to-resolve-sql-error-305-simplified/
date: 2016-10-30
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2016/10/sql-305-error.jpg?fit=1200%2C900&ssl=1
categories: ["SQL"]
tags: ["How to resolve -305 without Null indicator", "SQL -305 Error"]
type: post
lang: en
---

# How to Resolve -305 SQL Error in DB2 Quickly

Here is a resolution for -305 SQL error in DB2. The handling of Nulls is needed when aggregate functions are involved in [SQL Query](https://www.techopedia.com/definition/5736/query). Null means no data is returned.

Related[SQL -204 error resolution to resolve quickly](https://srinimf.com/2015/02/05/db2-sql-error-204-and-resolution/)

Advertisements

## [How to](https://srinimf.com/2015/02/05/db2-how-to-write-best-sql-queries/) Solve -305 SQL Error

You need to define indicator variable (NULL indicator) in SELECT statement of SQL if that contains one of the aggregate functions (MIN MAX, AVG, SUM).

## **Downloads**

[DB2 Tutorial](https://srinimf.com/2010/03/17/mainframe-db2-tutorial-for-software-developers-1-of-5/)

[SQL Complex Queries](https://srinimf.com/2012/07/25/complex-sql-queries/)

DB2 write a value to the indicator variable if it finds no data to process. Here are two resolutions. If the program is not set up with a indicator variable, an invalid -305 SQLCODE is returned.

### Resolution-1

In the COBOL program you need a logic to validate indicator variable . [Here is sample code how to create indicator variable.](https://www.ibm.com/docs/en/developer-for-zos/9.1.1?topic=applications-using-indicator-variables)

If the indicator variable value is less than zero, which means it is NULL value. If it is positive integer means, the data is truncated. [Here are the conditions to check indicator variables.](https://www.ibm.com/docs/en/db2-for-zos/11?topic=uhviss-determining-whether-retrieved-value-in-host-variable-is-null-truncated)

### Resolution-2

It is preferable to code the VALUE, COALESCE, or IFNULL function to alleviate any null indicator logic.

**For example:**

```
SELECT IFNULL(AVG(SALARY), 0) 
FROM EMP 
WHERE WORKDEPT = 'XYZ'
```

This will either return the average if rows are found, and zero if no rows were met in order to calculate an average.

Related Posts

- [How to Improve Your SQL Queries](https://srinimf.com/2015/02/05/db2-how-to-write-best-sql-queries/)DateFebruary 5, 2015
- [DB2 SQL Error Codes For Job Interviews](https://srinimf.com/2014/05/08/db2-zos-common-error-codes/)DateMay 8, 2014
- [These DB2 SQL Errors Asked in Many Mainframe Interviews](https://srinimf.com/2015/07/08/these-db2-sql-errors-asked-in-many-mainframe-interviews/)DateJuly 8, 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%2F30%2Fthe-best-way-to-resolve-sql-error-305-simplified%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2016%2F10%2Fsql-305-error.jpg%3Ffit%3D1200%252C900%26ssl%3D1&description=How%20to%20Resolve%20-305%20SQL%20Error%20in%20DB2%20Quickly)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2016/10/30/the-best-way-to-resolve-sql-error-305-simplified/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2016/10/30/the-best-way-to-resolve-sql-error-305-simplified/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2016/10/30/the-best-way-to-resolve-sql-error-305-simplified/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2016/10/30/the-best-way-to-resolve-sql-error-305-simplified/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Resolve%20-305%20SQL%20Error%20in%20DB2%20Quickly&body=https%3A%2F%2Fsrinimf.com%2F2016%2F10%2F30%2Fthe-best-way-to-resolve-sql-error-305-simplified%2F&share=email)
-

### Like this:

Like Loading…
