#sysdate 系统时间
#将时间转换成字符串
#to_char(timestamp,format) timestamp - 时间,format - 时间格式
#to_char(timestamp 'now','HH12:MI:SS')
#将字符串转成日期
#to_date(str,strformat) str - 时间字符串,strformat - 字符串格式
# to_date('05 Dec 2000', 'DD Mon YYYY')
#将字符串转换成时期-时间
#to_timestamp(string, format) str - 时间字符串,strformat - 字符串格式
#to_timestamp('05 Dec 2000', 'DD Mon YYYY')
#表 1. 用于 date/time 转换的模板
#模板:描述
#HH : 一天的小时数 (01-12)
#HH12:一天的小时数 (01-12)
#HH24:一天的小时数 (00-23)
#MI:分钟 (00-59)
#SS:秒 (00-59)
#SSSS:午夜后的秒 (0-86399)
#AM or A.M. or PM or P.M.: 正午标识(大写)
#am or a.m. or pm or p.m.: 正午标识(小写)
#Y,YYY: 带逗号的年(4 和更多位)
#YYYY: 年(4和更多位)
#YYY: 年的后三位
#YY: 年的后两位
#Y: 年的最后一位
#BC or B.C. or AD or A.D.: 年标识(大写)
#bc or b.c. or ad or a.d.: 年标识(小写)
#MONTH: 全长大写月份名(9字符)
#Month: 全长混合大小写月份名(9字符)
#month: 全长小写月份名(9字符)
#MON: 大写缩写月份名(3字符)
#Mon: 缩写混合大小写月份名(3字符)
#mon: 小写缩写月份名(3字符)
#MM: 月份 (01-12)
#DAY: 全长大写日期名(9字符)
#Day: 全长混合大小写日期名(9字符)
#day: 全长小写日期名(9字符)
#DY: 缩写大写日期名(3字符)
#Dy: 缩写混合大小写日期名(3字符)
#dy: 缩写小写日期名(3字符)
#DDD: 一年里的日子(001-366)
#DD: 一个月里的日子(01-31)
#D: 一周里的日子(1-7;SUN=1)
#W: 一个月里的周数
#WW: 一年里的周数
#CC: 世纪(2 位)
#J: Julian 日期(自公元前4712年1月1日来的日期)
#Q: 季度
#RM: 罗马数字的月份(I-XII;I=JAN)-大写
#rm: 罗马数字的月份(I-XII;I=JAN)-小写
#所有模板都都允许使用前缀和后缀修改器。模板里总是允许使用修改器。前缀 'FX' 只是一个全局修改器。
#表 2. 用于日期/时间模板 to_char() 的后缀 、
#后缀:描述:例子
#FM:填充模式前缀:FMMonth
#TH:大写顺序数后缀:DDTH
#th:小写顺序数后缀:DDTH
#FX:固定模式全局选项(见下面):FX Month DD Day
#SP:拼写模式(还未实现):DDSP
#用法须知:
#a、如果没有使用 FX 选项,to_timestamp 和 to_date 忽略空白。FX 必须做为模板里的第一个条目声明。
#b、反斜杠("\")必须用做双反斜杠("\\"),例如 '\\HH\\MI\\SS'。
#c、双引号('"')之间的字串被忽略并且不被分析。如果你想向输出写双引号,你必须在双引号前面放置一个双反斜杠('\\'),例如 '\\"YYYY Month\\"'。
#d、to_char 支持不带前导双引号('"')的文本,但是在双引号之间的任何字串会被迅速处理并且还保证不会被当作模板关键字解释(例如:'"Hello Year: "YYYY')。
#表 3. to_char 例子
#输入:输出
#to_char(now(),'Day, HH12:MI:SS') : 'Tuesday , 05:39:18'
#to_char(now(),'FMDay, HH12:MI:SS') : 'Tuesday, 05:39:18'
#oracle sql日期比较:
#在今天之前:
select * from up_date where key_time < to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
select * from up_date where key_time <= to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
#在今天只后:
select * from up_date where key_time > to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
select * from up_date where key_time >= to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
#精确时间:
select * from up_date where key_time = to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
#在某段时间内:
#1、key between t1 and t2
select * from up_date where key_time between to_date('2007-07-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
and to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss');
#2、key < t1 and key > t2
select * from up_date where key_time < to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
and key_time > to_date('2007-07-07 00:00:00','yyyy-mm-dd hh24:mi:ss');
#3、key <= t1 and key >= t2
select * from up_date where key_time <= to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
and key_time >= to_date('2007-07-07 00:00:00','yyyy-mm-dd hh24:mi:ss');
#获取系统时间 - 日期
to_date(to_char(sysdate, 'yyyy-mm-dd'),'yyyy-mm-dd')
#获取Oracle当前时间
#sysdate 系统的时间
#systimestamp 也是当前系统的时间戳
#current_timestamp 与时区设置有关,返回的秒是系统的,返回的日期和时间是根据时区转换过的。
#current_date 是对CURRENT_TIMESTAMP准确到秒的四舍五入。
select sysdate,systimestamp,current_date,current_timestamp from dual;
【数据库-Oracle】日期-时间 语法
最新推荐文章于 2023-11-20 09:14:12 发布