1. 程式人生 > >oracle學習筆記(九) SQL常用函數說明以及使用

oracle學習筆記(九) SQL常用函數說明以及使用

acl 時間戳 select hello 常用函數 uber rim alt default

SQL常用函數說明以及使用

以下補充以下常用的函數,更多的請看oracle函數API文檔

to_char

to_char(8.58,'9.99')
to_char(8.50,'9.00')
to_char(8.50,'9.99')
create table employee(
    id number,
    crete_date date timestamp default localtimestamp --毫秒級時間戳,插入一條數據會自動填寫 
);

to_date

--將2019-5-11字符串轉為date類型
select to_date('2019-5-11','yyyy-mm-dd') date1;
--2019年5月22日字符串轉為date類型
to_date('2019年5月22日','yyyy"年"mm"月"dd"日"') date2 from dual;

to_number

select to_nuber('1,234','9,999') --第二個參數,是格式

nvl 空值賦值

nvl(sal,0)  
--不為空,返回sal,如果為空,就返回0   
nvl2(sal,sal,0)     
--不為空,返回工資本身,為空,返回0     

字符串處理

  • ltrim
    刪除左邊空白符或指定字符
ltrim('   here',' ') --刪除空白符(空格)
ltrim('---hello world','-') --刪除“-”,最終結果顯示為hello world
ltrim('  hello world') --刪除空格
  • rtrim
    刪除右邊空白符或指定字符,與上面類似
  • trim
    刪除空白符或制定字符,與上面類似
  • substr
    截取字符

    decode 條件取值

decode(age,10,'少年',20,'青年',中年)
--相當於switch,age=10,返回少年,age=20,返回青年,其他的則返回中年

數學函數

  • abs
    絕對值
  • ceil
    返回較大的最小整數
ceil(7.6)
--返回8
  • floor
    返回較小的最大整數
round(7.6)
--返回7
  • round
    返回四舍五入的數值
Select round(100.256,2) from dual; --返回100.26
select round(100.256,3) from dual;  --返回100.256
  • trunc
    截取
Select trunc(100.256,2) from dual; --返回100.25
select trunc(100.256,3) from dual;  --返回100.256
  • power
    冪次方
  • mod
    取余數
  • sqrt
    平方根

oracle學習筆記(九) SQL常用函數說明以及使用