---
title: "Substring in DB2 Quick Tutorial"
description: "The substring is very important and mostly used function. You can get part of string from main string. This example gives you clear idea on how to use it especially in DB2."
url: https://srinimf.com/2017/01/02/the-best-substring-example-in-db2-for-quick-use/
date: 2017-01-02
modified: 2022-12-27
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2017/01/pexels-photo-4600489.jpeg?fit=1880%2C1253&ssl=1
categories: ["DB2 V10"]
tags: ["How to get Substring in DB2 an example", "Substring in DB2"]
type: post
lang: en
---

# Substring in DB2 Quick Tutorial

Substring function in DB2 plays critical role when you need to deal with Strings. The syntax is for DB2 substring given in the below a ready to use code.

Normally a function returns some value, but substring does not return any value- you can say it as ‘Zero length string’, but not to say as it is NULL.

## **What you will learn in this post**

- ## 1. You Will Learn Syntax for Substring Function
- ## 2. Example on How to Use Substring

This is actually a typical question. The other [functions in DB2](https://srinimf.com/2012/07/04/db2-sql-ltrim-rtrim-and-trim/)you can read before you move on.

>>-SUBSTRING–(charecter expression, start, Length, CODEUNITS16 or CODEUNITS32 or OCTETS)

## Substring example

FIRSTNAME is a VARCHAR(12) column in table T1. One of its values is the 6-character string ‘Jürgen’. When FIRSTNAME has the value ‘Jürgen’:

```
Function:                                   Returns:
   --------------------------------------------------------------
   SUBSTRING(FIRSTNAME,1,2,CODEUNITS32)       'Jü'  -- x'4AC3BC'
   SUBSTRING(FIRSTNAME,1,2,CODEUNITS16)       'Jü'  -- x'4AC3BC'
   SUBSTRING(FIRSTNAME,1,2,OCTETS)            'J '  -- x'4A20' (a truncated string)
   SUBSTRING(FIRSTNAME,8,CODEUNITS16)               -- a zero-length string
   SUBSTRING(FIRSTNAME,8,4,OCTETS)                  -- a zero-length string
```

**More explanation**

- CODEUNITS16, CODEUNITS32, or OCTETS- Specifies the string unit that is used to express start and length. If character-expression is a character string that is defined as bit data, CODEUNITS16 and CODEUNITS32 cannot be specified.
- CODEUNITS16 – Specifies that start and length are expressed in terms of 16-bit UTF-16 code units.
- CODEUNITS32 – Specifies that start and length are expressed in terms of 32-bit UTF-32 code units.
- OCTETS – Specifies that start and length are expressed in terms of bytes.

**Related Posts**

- [DB2: DIGITS and SUBSTR Functions](https://srinimf.com/2011/11/20/db2-sql-built-in-functions/)DateNovember 20, 2011
- [SQL Query: How to Use LOCATE and Substring Correctly](https://srinimf.com/2021/04/18/sql-query-how-to-use-locate-and-substring-correctly/)DateApril 18, 2021
- [How to Create SQL UDF Quickly](https://srinimf.com/2017/10/12/spilt-string-sql-logic-to-create-function/)DateOctober 12, 2017

### 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%2F2017%2F01%2F02%2Fthe-best-substring-example-in-db2-for-quick-use%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2017%2F01%2Fpexels-photo-4600489.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=Substring%20in%20DB2%20Quick%20Tutorial)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2017/01/02/the-best-substring-example-in-db2-for-quick-use/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2017/01/02/the-best-substring-example-in-db2-for-quick-use/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2017/01/02/the-best-substring-example-in-db2-for-quick-use/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2017/01/02/the-best-substring-example-in-db2-for-quick-use/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Substring%20in%20DB2%20Quick%20Tutorial&body=https%3A%2F%2Fsrinimf.com%2F2017%2F01%2F02%2Fthe-best-substring-example-in-db2-for-quick-use%2F&share=email)
-

### Like this:

Like Loading…
