查询当前系统日期:sql
Oracle: select to_char(sysdate, 'yyyy-mm-dd')session
Mysql:select current_date或者 select curdate()ide
查询当前系统时间:unix
Oracle: select to_char(sysdate, 'hh24:mi:ss')orm
Mysql: select curtime()或者 select current_time字符串
查询系统日期和时间:string
Oracle: select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')it
Mysql: select sysdate() 或者 select now()io
时间戳:ast
Oracle: select systimestamp
Mysql: select current_timestamp或者select current_timestamp()
字符串截取:
Oracle: substr(ch,pos,length)
注:pos 0 ,1均可以
Myslq: substr(str,pos,len) 或者 substring(str,pos,len)
注:pos从1开始
Oracle:
日期转字符串:
to_char(date,format)
ex: select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss')
select to_char(systimestamp, 'yyyy-mm-dd hh24:mi:ss.ff')
字符串转timestamp:
ex: to_timestamp('2017-09-29 10:49:42.322940', 'yyyy-mm-dd hh24:mi:ss.ff')
to_timestamp(to_char(systimestamp, 'yyyy-mm-dd hh24:mi:ss.ff'),'yyyy-mm-dd hh24:mi:ss.ff')
字符串转date
to_date(ch,fmt)
ex: select to_date('2017-09-29 10:24:15', 'yyyy-mm-dd hh24:mi:ss')
date转timestamp:
ex: select cast(sysdate as timestamp)
select cast(to_date('2017-09-29 10:34:01', 'yyyy-mm-dd hh24:mi:ss') as timestamp)
时间戳转date:
ex: to_date('19700101','yyyymmdd') + (时间戳) / 86400 +to_number(substr(tz_offset(sessiontimezone), 1, 3)) / 24
前一个月:
ex: select add_months(sysdate, -1) from dual
前一日:
ex: select sysdate - 1 from dual
前一天前一小时前一分钟前一秒:
ex: select '前一天前一小时前一分钟前一秒' TITLE, to_char(SYSDATE - 1 - 1 / 24 - 1 / 24 / 60 - 1 / 24/ 60 / 60, 'yyyy-mm-dd hh24:mi:ss') TIME
月份之差:
ex: select months_between(sysdate, to_date('2017-05-29', 'yyyy-mm-dd')) from dual
Mysql:
UNIX时间戳转换为日期:
from_unixtime(unix_timestamp,format)
参数:UNIX 时间戳返回值:字符串
ex: from_unixtime (1506648322, '%Y-%m-%d %H:%i:%s' )
日期转换为UNIX时间戳:
unix_timestamp(date)
若是没有参数调用,返回一个Unix时间戳(从'1970-01-01 00:00:00'GMT开始的秒数)
若是一个date参数被调用,返回从'1970-01-01 00:00:00' GMT开始到date的秒数值
ex: select unix_timestamp()
date转字符串:
date_format(date,format)
ex: select date_format(now(), '%Y-%m-%d %H:%i:%s')
字符串转date:
str_to_date(str,format)
ex: select str_to_date('2017-10-27 08:48:50', '%Y-%m-%d %H:%i:%s')
字符串转时间戳:
ex: select unix_timestamp('2017-10-27 08:48:50')
结果:1509065330
时间戳转字符串:
ex: select from_unixtime(1509065330, '%Y-%m-%d %H:%i:%s')
结果:2017-10-27 08:48:50
date转时间戳:
ex: select unix_timestamp(now())
结果:1509066003
时间戳转date:
ex: select from_unixtime(1509066003)
结果:2017-10-27 09:00:03
日期减去一个时间间隔:
date_sub()
ex: date_sub(date, interval 1 day)
date_sub(date, interval -1 day)
date_sub(date, interval 1 month)
date_sub(date, interval 1 year)