Thursday, 22 May 2014

How to convert data types on DB2

How to convert data types on DB2

cast functions to convert data type or db2 functions to convert data types
On DB2 you have two ways to convert DATA TYPES: 
We are going to use the table below for our examples:
db2 describe table example
Output:                                                             
Column name                     schema    Data type name      Column Length     Scale      Nulls
----------------------------------- ------------ ----------------------- ----------------------------- --------- --------
COD                                     SYSIBM      INTEGER                             4                        0        Yes   
NAME                                  SYSIBM      VARCHAR                         40                        0        Yes   
ENTRANCE                       SYSIBM      DATE                                    4                         0        Yes   
VALUE                                SYSIBM      DOUBLE                               8                        0        Yes   

Data:

db2 select * from example
COD         NAME                                                   ENTRANCE    VALUE                  
----------- ---------------------------------------------- --------------------- ------------------------
     1                                              William                   10/15/2009   +3.22900000000000E+002
     2                                                       38                   12/23/2009   +4.59000000000000E+001
     3                                             Barbara                  02/03/2009   +1.43000000000000E+000
234                                                        40                  02/30/2009   +2.30000000000000E+001



FIRST WAY - USING FUNCTION CAST


Example A
db2 "select cod, cast(name as int) as name from example where name in ('38','40')"

Example B
db2 "select cast(cod as char) from example"


SECOND WAY



Example A
db2 "select cod, int(name) as name from example where name in ('38','40')"

Example B

db2 "select char(cod) from example"

No comments:

Post a Comment