oracle,mysql中字符串,date,timestamp转换

来源:互联网 发布:linux vi如何保存退出 编辑:程序博客网 时间:2024/06/11 19:00

查询当前系统日期:

Oracle: select to_char(sysdate, 'yyyy-mm-dd')

Mysql:select current_date或者 select curdate()

 

查询当前系统时间:

Oracle: select to_char(sysdate, 'hh24:mi:ss')

Mysql: select curtime()或者 select current_time

 

查询系统日期和时间:

Oracle: select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')

Mysql: select sysdate() 或者 select now()

 

时间戳:

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)

注:pos1开始

 

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')

 

datetimestamp

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)


原创粉丝点击