Db2 CAST

Summary: in this tutorial, you will learn how to use the Db2 CAST expression to convert a value of a type to another.

Db2 CAST expression overview

The following query adds the number 1 and string ‘2’:

SELECT
    1 + '2' result 
FROM
    sysibm.sysdummy1;
Code language: SQL (Structured Query Language) (sql)

The result is number 3:

RESULT                                     
-------
3                      
Code language: SQL (Structured Query Language) (sql)

Db2 has implicitly converted the string ‘2’ to an integer 2 due to the add operator (+).

The following example concatenates the number 1 with the string ‘2’ using the concatenate operator (||)

SELECT
    1 || '2' result
FROM 
    sysibm.sysdummy1;
Code language: SQL (Structured Query Language) (sql)

The result is as follows:

RESULT 
------ 
12     
Code language: SQL (Structured Query Language) (sql)

In this example, Db2 implicitly converted the number 1 to the string ‘1’ and concatenated it with the string ‘2’.

By default, when you use values of different types in an expression or pass the values of different types to functions, Db2 will try to convert the value of one type to another, depending on the context.

If the conversion fails, Db2 will issue an error:

Invalid character found in a character string argument of the function "DECFLOAT".. SQLCODE=-420, SQLSTATE=22018, DRIVER=3.69.56
Code language: SQL (Structured Query Language) (sql)

To explicitly convert a value of one type to another, you use the CAST expression with the following syntax:

CAST(value AS data_type)
Code language: SQL (Structured Query Language) (sql)

In this syntax, you specify the value and the data type to which you want to convert the value.

Db2 CAST examples

Let’s take some examples of using the CAST expression.

1) Converting a decimal to an integer example example

This example uses the CAST expression to convert a decimal to an integer:

SELECT 
    CAST(10.7 AS INT) result
FROM 
    sysibm.sysdummy1;
Code language: SQL (Structured Query Language) (sql)

Here is the result:

RESULT      
----------- 
10          
Code language: SQL (Structured Query Language) (sql)

2) Converting a decimal to decimal with a smaller scale example

This example uses the CAST expression to convert a decimal number from DEC(5,3) to DEC(4,2):

SELECT 
    CAST(10.235 AS DEC(4,2)) result
FROM 
    sysibm.sysdummy1;
Code language: SQL (Structured Query Language) (sql)

The result is as follows:

RESULT 
------ 
10.23
Code language: SQL (Structured Query Language) (sql)

3) Converting a timestamp to a time example

The following example uses the CAST expression to convert a timestamp to a time:

SELECT 
    CAST(CURRENT TIMESTAMP AS TIME) result
FROM 
    sysibm.sysdummy1;
Code language: SQL (Structured Query Language) (sql)

Here is the output:

RESULT                    
---------
14:52:05        
Code language: SQL (Structured Query Language) (sql)

4) Converting a timestamp to a date example

The following example uses the CAST expression to convert a timestamp to a date:

SELECT 
    CAST(CURRENT TIMESTAMP AS DATE) result
FROM 
    sysibm.sysdummy1;
Code language: SQL (Structured Query Language) (sql)

Here is the output:

RESULT                    
-----------
2019-06-13  
Code language: SQL (Structured Query Language) (sql)

5) Converting a string to a date example

This example uses the CAST expression to convert a string to a date:

SELECT 
    CAST('2019-06-25' AS DATE) result
FROM 
    sysibm.sysdummy1;
Code language: SQL (Structured Query Language) (sql)

The output is as follows:

RESULT                    
------------
2019-06-25          
Code language: SQL (Structured Query Language) (sql)

In this tutorial, you’ve learned how to use the Db2 CAST expression to convert a value of one type to another.

Was this tutorial helpful ?