DB2常用函式詳解(二) 型別轉換函式

2021-06-23 01:07:55 字數 2982 閱讀 3565

cast表示式用來轉換型別使用

select

cast

(current

time

aschar

(8) ) 

from

sysibm.sysdummy1  

select cast(current time as char(8) ) from sysibm.sysdummy1

1、轉換為字串型別:

char函式

語法:char(arg) 

char函式返回日期時間型、字串、整數、十進位制或雙精度浮點數的字串表示。

eg:   

select

char

(salary) 

from

test  

eg:

select char(salary) from test

chr函式

語法:chr(arg) 

chr函式返回由引數指定的ascii碼的字元,引數可以是integer或smallint。

eg:   

select

chr(167) 

from

test  

eg:

select chr(167) from test

varchar函式

語法:varchar(arg,length) 

varchar函式返回字串、日期型、圖形串的可變長度的字串表示。

eg:   

select

varchar

(name

,50) 

from

test   

--50為擷取長度,如果name字串的長度大於50,則返回「sql0445w 值已被截斷。sqlstate=01004」。

eg:

select varchar(name,50) from test

--50為擷取長度,如果name字串的長度大於50,則返回「sql0445w 值已被截斷。sqlstate=01004」。

digits函式

語法:digits() 

digits函式返回smallint、integer、bigit或者decimal引數的字串值。

eg:   

select

digits(id) 

from

test  

eg:

select digits(id) from test

2、轉換為數字型別:

double、float函式

語法:double()、float() 

double、float函式如果引數是乙個數字表示式,返回與其對應的浮點數,否則返回錯誤**。

eg:   

select

double

('4569'

) from

test  

eg:

select double('4569') from test

int函式

語法:int() 

int函式返回整型常量中的數字、字串或者日期、時間的整數表示。

eg:   

select

int(

'111'

) from

test  

eg:

select int('111') from test

bigint函式

語法:bigint() 

bigint函式返回整型常量中的數字、字串或者時間戳的64位長整數表示。

eg:   

select

bigint

('111'

) from

test  

eg:

select bigint('111') from test

smallint函式

語法:smallint() 

smallint函式返回整型常量中的數字、字串短整數表示。

eg:   

select

smallint

('111'

) from

test  

eg:

select smallint('111') from test

real函式

語法:real() 

real函式返回乙個數值的單精度浮點數表示。

eg:   

select

real

(10) 

from

test  

eg:

select real(10) from test

dec[imal]函式

語法:dec[imal]() 

dec[imal]函式返回乙個數值、decimal的字串、integer的字串、float-point的字串、日期、時間或時間戳的decimal數值。

eg:   

select

dec(10) 

from

test  

eg:

select dec(10) from test

hex函式

語法:hex() 

hex函式返回乙個字串的值的16進製表示。

eg:   

select

hex(10) 

from

test  

eg:

select hex(10) from test

floor函式

語法:floor() 

floor函式小於或等於引數的最大整數。

eg:   

select

floor(10.50) 

from

test 

DB2常用函式詳解(二) 型別轉換函式

cast表示式用來轉換型別使用 select cast current time aschar 8 from sysibm.sysdummy1 select cast current time as char 8 from sysibm.sysdummy1 1 轉換為字串型別 char函式 語法 c...

struts2學習筆記(二) 型別轉換

基本型別 boolean和boolean,char和chracter,int和integer,long和long,float和float,double和double,date 使用者輸入為yyyy mm dd 集合型別 對於list和陣列,元素為string,如果是復合型別需要提供自定義轉換器。其他...

前端面試系列之二 型別轉換

0 或 0 0,nan false undefined null這幾個是false 其他的全都是true。if a b 的正確理解方式是 a b 進行表示式求值後,然後在轉換為boolean型別。是短路語法,求值後不一定是乙個boolean型別。短路表示式 1 只要 前面為false,不管 後面是t...