Oracle 日期格式

2021-06-01 23:49:10 字數 4116 閱讀 5141

oracle 獲取當前時間的月份

select extract(year from sysdate) from dual;

--oracle 日期引數

--今天

select to_char(sysdate,'yyyy-mm-dd') from dual

--昨天

select to_char(sysdate - 1,'yyyy-mm-dd') from dual

--明天

select to_char(sysdate + 1,'yyyy-mm-dd') from dual

--本週第一天,注意是週日還是周一

select to_char(trunc(sysdate,'day'),'yyyy-mm-dd') from dual

--本週最後一天,注意是週日還是周一

select to_char(trunc(sysdate,'day') + 6,'yyyy-mm-dd') fromdual

--上週第一天,注意是週日還是周一

select to_char(trunc(sysdate,'day') -7 ,'yyyy-mm-dd') fromdual

--上週最後一天,注意是週日還是周一

select to_char(trunc(sysdate,'day') -1 ,'yyyy-mm-dd') fromdual

--當月第一天

select to_char(trunc(sysdate,'month'),'yyyy-mm-dd') fromdual

--當月最後一天

select to_char(add_months(trunc(sysdate,'month'), 1)-1,'yyyy-mm-dd') from dual

--上月第一天

select to_char(add_months(trunc(sysdate,'month'), - 1),'yyyy-mm-dd') from dual

--上月最後一天

select to_char(trunc(sysdate,'month') -1,'yyyy-mm-dd') fromdual

--下月第一天

select to_char(add_months(trunc(sysdate,'month'), 1),'yyyy-mm-dd') from dual

--下月最後一天

select to_char(add_months(trunc(sysdate,'month'), 2)-1,'yyyy-mm-dd') from dual

--當年第一天

select to_char(trunc(sysdate,'year'),'yyyy-mm-dd') from dual

--當年最後一天

select to_char(add_months(trunc(sysdate,'year') , + 12) -1,'yyyy-mm-dd') from dual

--去年第一天

select to_char(add_months(trunc(sysdate,'year') , -12),'yyyy-mm-dd') from dual

--去年最後一天

select to_char(trunc(sysdate,'year') - 1,'yyyy-mm-dd') fromdual

--明年第一天

select to_char(add_months(trunc(sysdate,'year') , +12),'yyyy-mm-dd') from dual

--明年最後一天

select to_char(add_months(trunc(sysdate,'year') , + 24) -1,'yyyy-mm-dd') from dual

--年齡(按周歲)

select trunc(months_between(sysdate,to_date('2000-01-01','yyyy-mm-dd')) / 12) from dual;

--年齡(自然年差)

select cast(to_char(sysdate,'yyyy') as integer) -cast(to_char(to_date('2000-01-01','yyyy-mm-dd'),'yyyy') as integer) from dual;

select extract(month from sysdate) from dual

使用utl_file包來將表的資料儲存到外部檔案

該實驗的目的是將表的資料按照我們的格式寫入到作業系統的檔案,利用了資料庫提供的包,將表中的資料儲存在文字檔案。

在初始化引數檔案中加入下面一行

utl_file_dir=/home/oracle/bk

重新啟動資料庫

show parameter utl_file_dir

name          type      value

utl_file_dir  string    /home/oracle/bk

案例1:資料寫入到文字中

declare

v_filehandleutl_file.file_type;

begin

v_filehandle:=utl_file.fopen('/home/oracle/bk','output.txt','w');

utl_file.putf(v_filehandle,'salary report: generated on%s\n', sysdate);

utl_file.new_line(v_filehandle);

utl_file.putf (v_filehandle,'%s\n','hello ');

utl_file.putf (v_filehandle, 'department:%s\n','world ');

utl_file.putf(v_filehandle,'aaaa%sbbb%sccc%sddd%seee%s','1','2','3','4','5');

utl_file.fclose (v_filehandle);

end;

其中/n為換行

%s為替代字元,將來會被後面的1到5個引數替代,預設值為null

mew_line過程建立乙個新的空行。

案例2:將dept表的資料匯入到文字中

declare

v_filehandle utl_file.file_type;

begin

v_filehandle:=utl_file.fopen('/home/oracle/bk','output.txt','w');

utl_file.putf (v_filehandle,'表dept的文字資料,匯出時間為:%s\n', sysdate);

utl_file.new_line(v_filehandle);

for i in(select * from dept)loop

utl_file.putf (v_filehandle,'%s ,%s, %s\n',i.deptno,i.dname,i.loc);

end loop;

utl_file.fclose (v_filehandle);

end;

案例3:將emp表的資料匯入到文字中

declare

v_filehandleutl_file.file_type;

begin

v_filehandle:=utl_file.fopen('/home/oracle/bk','output.txt','w');

utl_file.putf (v_filehandle,'表emp的文字資料,匯出時間為:%s\n', sysdate);

utl_file.new_line(v_filehandle);

for i in(select * from emp)loop

utl_file.putf (v_filehandle,'%s| %s |%s| %s| %s

|',i.empno,i.ename,i.job,nvl(i.mgr,-1),i.hiredate);

utl_file.putf (v_filehandle,'%s| %s |%s\n',i.sal,nvl(i.comm,-1),i.deptno);

end loop;

utl_file.fclose (v_filehandle);

end;

Oracle日期格式

日期處理完全版 to date格式 day dd number 12 dy abbreviated fri day spelled out friday ddspth spelled out,ordinal twelfth month mm number 03 mon abbreviated mar...

Oracle日期格式

sysdate 2008 12 29 08 27 格式marsk 回傳格式 demo 結果y yy yyy 西元年最後一位數,兩位數,三位數 select to char sysdate,yyy from dual 008year 拼寫出西元年 select to char sysdate,year...

Oracle日期格式

sysdate 2008 12 29 08 27 格式marsk 回傳格式 demo 結果y yy yyy 西元年最後一位數,兩位數,三位數 select to char sysdate,yyy from dual 008 year 拼寫出西元年 select to char sysdate,yea...