Oracle 轉換函式

2021-09-06 22:50:47 字數 3617 閱讀 3719

oracle 轉換函式

-- to_char(date|number ) fmt:格式內容,返回的字串是什麼格式的,在此處指定;nlsparams:指定國家語言設定引數,設定一些數值中的字元,不同國家不同,預設按當前會話的設定

-- 將日期轉換成字串

-- fm:壓縮因轉換後字串中多填充的空格或者數字前面填充的0

select employee_id,to_char(hire_date,'fmdd month yyyy') from employees;

select employee_id,to_char(hire_date,'dd-mm-yyyy') from employees;

-- 特殊字元必須將特殊字元使用英文的雙引號,引起來

select employee_id,to_char(hire_date,'yyyy"年"mm"月"dd"日"') from employees;

-- 將數字轉換成字串

-- to_date(char  ) 將字串轉換成日期型別

-- fx:精確匹配,要轉換的字串要嚴格的按照右邊fmt的格式,否則就會報錯

select last_name,hire_date

from employees

where hire_date>to_date('2007-09-10','fxyyyy-mm-dd');

-- to_number(char )將字串轉換成數值型別

select to_number('12,345.05','999,999.00') from dual;

-- 函式巢狀

-- oracle 可以達到任意深度的函式巢狀,巢狀的計算是從內到外

select last_name,upper(concat(substr(last_name,1,3),'_zh'))

from employees;

-- 通用函式

-- nvl(column|expression1,expression2) 將空值轉換成目標值 expression2:如果expression1為空則返回expression2

-- 注:expression1和expression2的型別一定要一致

select last_name,salary,commission_pct,(salary*12)+(salary*12*nvl(commission_pct,0)) as "all_salary"

from employees;

-- nvl2(expr1,expr2,expr3) 將空格轉換為已指定的值 ,如果expr1不為空則返回 expr2,如果expr1為空則返回expr3

-- 注:expr1可以是任意型別,expr2和expr3可以是除long以外的任意型別

select last_name,salary,commission_pct,nvl2(commission_pct,'sal+comm','sal') as "income"

from employees

where department_id in (50,80);

-- nullif(expr1,expr2) 判斷expr1和expr2是否相等,如果相等則返回null,如果不等則返回expr1

-- 注:expr1和expr2不能直接傳入null

select length(first_name) as "expr1",length(last_name) as "expr2",nullif(length(first_name),length(last_name)) as "result"

from employees;

-- coalesce(expr1,expr2,expr3,...exprn) 返回表示式中第乙個不為空的表示式,如果expr1為空則判斷expr2是否為空,如果不為空則返回expr2,否則較驗expr3...

-- 如果所有表示式都為空,則返回空;所有表示式都要是統一的資料型別

select last_name,coalesce(to_char(commission_pct),to_char(manager_id),'no commission and no manager')

from employees;

-- case expr when expr1 then return1

-- [when expr2 then return2

-- ...

-- when exprn then returnn]

-- else else_expr end

select last_name,job_id,salary,case job_id when 'it_prog' then 1.10*salary

when 'st_clerk' then 1.15*salary

when 'sa_rep' then 1.20*salary

else salary end as "new salary"

from employees;

select last_name,salary,(case when salary<5000 then 'low'

when salary<10000 then 'medinum'

when salary<20000 then 'good'

else 'excellent' end) as "salary level"

from employees;

-- decode(expr,expr1,return1

-- expr2,return2

-- expr3,return3

-- ...

-- default)

select last_name,job_id,salary,decode(job_id,'it_prog',1.10*salary,

'st_clerk',1.15*salary,

'sa_rep',1.20*salary,

salary) as "new salary"

from employees;

oracle函式 轉換函式

1 asciistr str 說明 將任意字符集的字串轉換為當前資料庫例項對應的ascii字串。select asciistr 中華民族 from dual 備註 和該函式相似的有ascii,它是取得字串第乙個字元的ascii碼,後面的字元不管 chr是將ascii碼轉換為對應的字元。2 cast ...

oracle轉換函式

我的日誌 分類 oracle 應用 開發聚寶盆 2007.5.29 17 34 作者 獨上層樓 說明 將任意字符集的字串轉換為當前資料庫例項對應的 ascii 字串。select asciistr 中華民族 from dual 備註 和該函式相似的有 ascii,它是取得字串第乙個字元的 ascii...

Oracle 轉換函式

oracle函式知識 轉換函式,轉換函式將值從一種資料型別轉換為另外一種資料型別。常用的轉換函式有 1.to char d n fmt 把日期和數字轉換為制定格式的字串。fmt是格式化字串,日期的格式化字串前面已經學習過。演示 to char對日期的處理 sql select to char sys...