zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

Mysql、Oracle 中的日期格式化比较

mysqlOracle日期 比较 格式化
2023-09-11 14:20:19 时间

1.Mysql、Oracle 中的日期格式化比较

Mysql中使用 date_format函数和str_to_date()
date_format(create_time,’%Y-%m-%d %H:%i:%s’)
date_format(‘2018-11-12 16:08:09’,’%Y-%m-%d %H:%i:%s’)

Oracle中使用 to_char函数和to_date函数:to_char(create_time,‘yyyy-MM-dd hh24:mi:ss’)
to_char(systimestamp,‘yyyy-MM-dd hh24:mi:ss’)
或 to_date(‘2018-11-12 16:08:09’,‘yyyy-MM-dd hh24:mi:ss’)

2.Mysql、Oracle 中的系统时间比较

Mysql中 系统时间是:now()
Oracle中 系统时间是 : sysdate

3.mysql 日期 字符串 时间戳 互转

时间转字符串

select DATE_FORMAT(now(),'%Y-%m-%d %H:%i:%s')
-- 结果:2020-07-07 15:04:48

时间转时间戳

select unix_timestamp(now())
-- 结果:1594105623

字符串转时间

select str_to_date('2020-07-07','%Y-%m-%d')
-- 结果:2020-07-07

字符串转时间戳

select unix_timestamp('2020-07-07')
-- 结果:1594051200

时间戳转时间

select from_unixtime(1594105623)
-- 结果:2020-07-07 15:07:03

时间戳转字符串

select from_unixtime(1594105623,'%Y-%m-%d')
-- 结果:2020-07-07

4.oracle 日期 字符串 时间戳 互转

时间转字符串

select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual;
-- 结果:2020-11-19 14:13:56
select to_char(sysdate,'yyyy"年"MM"月"dd"日" HH24:mi:ss') from dual;
-- 结果:2020年11月19日 14:13:56

字符串转时间

select to_date('2020-07-07','yyyy-MM-dd') from dual;
-- 结果:2020-07-07

时间戳转时间

SELECT TO_CHAR(时间戳的那一列 / (1000 * 60 * 60 * 24) +  
       TO_DATE('1970-01-01 08:00:00', 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD') 
       AS createTime FROM 表名 ;  
-- 原理: 用to_date函数将字符串’1970-01-01 08:00:00’转换为日期作为起始时间,同时将时间戳转换为天数,在此基础上将两者相加,即为该时间戳对应的具体日期时间,最后截取我们需要的日期部分,并且取名为createTime。