Sql Server、Oracle、MySQL 日期格式化函数处理


目录
  • Sql Server
  • Oracle
  • MySql

Sql Server

CONVERT ( '取数长度' , '时间' , '类型')


查询对应时间: 2021-03-17T19:18:18.007
Select CONVERT(varchar(100), GETDATE(), 0)   --  03 17 2021  7:18PM
Select CONVERT(varchar(100), GETDATE(), 1)   --  03/17/21
Select CONVERT(varchar(100), GETDATE(), 2)   --  21.03.17
Select CONVERT(varchar(100), GETDATE(), 3)   --  2017-3-21
Select CONVERT(varchar(100), GETDATE(), 4)   --  17.03.21
Select CONVERT(varchar(100), GETDATE(), 5)   --  2017-3-21
Select CONVERT(varchar(100), GETDATE(), 6)   --  17 03 21
Select CONVERT(varchar(100), GETDATE(), 7)   --  03 17, 21
Select CONVERT(varchar(100), GETDATE(), 8)   --  19:18:18
Select CONVERT(varchar(100), GETDATE(), 9)   --  03 17 2021  7:18:18:007PM
Select CONVERT(varchar(100), GETDATE(), 10)  --  03-17-21
Select CONVERT(varchar(100), GETDATE(), 11)  --  2021-3-17
Select CONVERT(varchar(100), GETDATE(), 12)  --  210317
Select CONVERT(varchar(100), GETDATE(), 13)  --  17 03 2021 19:18:18:007
Select CONVERT(varchar(100), GETDATE(), 14)  --  19:18:18:007
Select CONVERT(varchar(100), GETDATE(), 20)  --  2021-3-17 19:18
Select CONVERT(varchar(100), GETDATE(), 21)  --  18:18.0
Select CONVERT(varchar(100), GETDATE(), 22)  --  03/17/21  7:18:18 PM
Select CONVERT(varchar(100), GETDATE(), 23)  --  2021-3-17
Select CONVERT(varchar(100), GETDATE(), 24)  --  19:18:18
Select CONVERT(varchar(100), GETDATE(), 25)  --  18:18.0
Select CONVERT(varchar(100), GETDATE(), 100) --  03 17 2021  7:18PM
Select CONVERT(varchar(100), GETDATE(), 101) --  03/17/2021
Select CONVERT(varchar(100), GETDATE(), 102) --  2021.03.17
Select CONVERT(varchar(100), GETDATE(), 103) --  17/03/2021
Select CONVERT(varchar(100), GETDATE(), 104) --  17.03.2021
Select CONVERT(varchar(100), GETDATE(), 105) --  17-03-2021
Select CONVERT(varchar(100), GETDATE(), 106) --  17 03 2021
Select CONVERT(varchar(100), GETDATE(), 107) --  03 17, 2021
Select CONVERT(varchar(100), GETDATE(), 108) --  19:18:18
Select CONVERT(varchar(100), GETDATE(), 109) --  03 17 2021  7:18:18:007PM
Select CONVERT(varchar(100), GETDATE(), 110) --  03-17-2021
Select CONVERT(varchar(100), GETDATE(), 111) --  2021-3-17
Select CONVERT(varchar(100), GETDATE(), 112) --  20210317
Select CONVERT(varchar(100), GETDATE(), 113) --  17 03 2021 19:18:18:007
Select CONVERT(varchar(100), GETDATE(), 114) --  19:18:18:007
Select CONVERT(varchar(100), GETDATE(), 120) --  2021-3-17 19:18
Select CONVERT(varchar(100), GETDATE(), 121) --  18:18.0
Select CONVERT(varchar(100), GETDATE(), 126) --  2021-03-17T19:18:18.007
Select CONVERT(varchar(100), GETDATE(), 130) --  4 ????? 1442  7:18:18:007PM
Select CONVERT(varchar(100), GETDATE(), 131) --  4/08/1442  7:18:18:007PM


Oracle

-- 字符串转时间类型
SELECT to_date(SYSDATE,'yyyy-mm-dd hh24:mi:ss') FROM dual;
-- 时间转指定格式字符串
SELECT to_char(SYSDATE,'yyyy"年"mm"月"dd"日"') FROM DUAL;


-- 年 季  月 日 取值
select to_char(sysdate,'yyyy') from dual; --年
select to_char(sysdate,'Q' from dual; --季
select to_char(sysdate,'mm') from dual; --月
select to_char(sysdate,'dd') from dual; --日

-- Oracle 时间
SELECT SYSDATE FROM  dual;  -- 系统时间   
SELECT SYSTIMESTAMP FROM  dual;  -- 当前系统时间戳
SELECT CURRENT_TIMESTAMP FROM  dual;  -- 与时区设置有关,返回的秒是系统的,返回的日期和时间是根据时区转换过的
SELECT current_date FROM  dual;  -- 是对CURRENT_TIMESTAMP准确到秒的四舍五入


select TO_DATE('19700101','yyyymmdd') + 1235728935/86400 +TO_NUMBER(SUBSTR(TZ_OFFSET(sessiontimezone),1,3))/24 from dual,其中1235728935就是unix/linux时间戳,转换完之后就表示为 2009-2-27 18:02:15。
反过来也一样,还是要考虑时区
select (to_date('2009-2-27 18:02:15','yyyy-mm-dd hh24:mi:ss') - to_date('1970-1-1','yyyy-mm-dd'))*86400- TO_NUMBER(SUBSTR(TZ_OFFSET(sessiontimezone),1,3))*3600 from dual




-- 时间差计算
DECLARE
START_DATE DATE;
END_DATE DATE;
BEGIN
  START_DATE := SYSDATE - 1 ;
  END_DATE := SYSDATE  ;
  dbms_output.put_line('相差天数 :' || ROUND(TO_NUMBER(END_DATE - START_DATE)));
  dbms_output.put_line('相差小时数 :' || ROUND(TO_NUMBER(END_DATE - START_DATE) * 24));
  dbms_output.put_line('相差分钟数 :' || ROUND(TO_NUMBER(END_DATE - START_DATE) * 24 * 60));
  dbms_output.put_line('相差秒数 :' || ROUND(TO_NUMBER(END_DATE - START_DATE) * 24 * 60 * 60));
  dbms_output.put_line('相差毫秒数 :' || ROUND(TO_NUMBER(END_DATE - START_DATE) * 24 * 60 * 60 * 1000));
END;



-- 时间日期加减
select sysdate,add_months(sysdate,12) from dual; --加1年
select sysdate,add_months(sysdate,1) from dual; --加1月
select sysdate,to_char(sysdate+7,'yyyy-mm-dd HH24:MI:SS') from dual; --加1星期
select sysdate,to_char(sysdate+1,'yyyy-mm-dd HH24:MI:SS') from dual; --加1天
select sysdate,to_char(sysdate+1/24,'yyyy-mm-dd HH24:MI:SS') from dual; --加1小时
select sysdate,to_char(sysdate+1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual; --加1分钟
select sysdate,to_char(sysdate+1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; --加1秒
--------------------------------------------------------------------------------------
select sysdate,add_months(sysdate,-12) from dual; --减1年
select sysdate,add_months(sysdate,-1) from dual; --减1月
select sysdate,to_char(sysdate-7,'yyyy-mm-dd HH24:MI:SS') from dual; --减1星期
select sysdate,to_char(sysdate-1,'yyyy-mm-dd HH24:MI:SS') from dual; --减1天
select sysdate,to_char(sysdate-1/24,'yyyy-mm-dd HH24:MI:SS') from dual; --减1小时
select sysdate,to_char(sysdate-1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual; --减1分钟
select sysdate,to_char(sysdate-1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; --减1秒

MySql
 select date_format(moment,'%Y%m%d %H:%i:%s')

* %S, %s 两位数字形式的秒( 00,01, . . ., 59)
* %i 两位数字形式的分( 00,01, . . ., 59)
* %H 两位数字形式的小时,24 小时(00,01, . . ., 23)
* %h, %I 两位数字形式的小时,12 小时(01,02, . . ., 12)
* %k 数字形式的小时,24 小时(0,1, . . ., 23)
* %l 数字形式的小时,12 小时(1, 2, . . ., 12)
* %T 24 小时的时间形式(h h : m m : s s)
* %r 12 小时的时间形式(hh:mm:ss AM 或hh:mm:ss PM)
* %p AM 或P M %W 一周中每一天的名称( S u n d a y, Monday, . . ., Saturday)
* %a 一周中每一天名称的缩写( Sun, Mon, . . ., Sat)
* %d 两位数字表示月中的天数( 00, 01, . . ., 31)
* %e 数字形式表示月中的天数( 1, 2, . . ., 31)
* %D 英文后缀表示月中的天数( 1st, 2nd, 3rd, . . .)
* %w 以数字形式表示周中的天数( 0 = S u n d a y, 1=Monday, . . ., 6=Saturday)
* %j 以三位数字表示年中的天数( 001, 002, . . ., 366)
* %U 周(0, 1, 52),其中Sunday 为周中的第一天
* %u 周(0, 1, 52),其中Monday 为周中的第一天
* %M 月名(J a n u a r y, February, . . ., December)
* %b 缩写的月名( J a n u a r y, February, . . ., December)
* %m 两位数字表示的月份( 01, 02, . . ., 12)
* %c 数字表示的月份( 1, 2, . . ., 12) 
  %Y 四位数字表示的年份
* %y 两位数字表示的年份 %% 直接值“%”


SELECT curdate(); -- 获取当前日期
select curtime(); -- 获取当前时间
select now(); -- 取得当前日期时间
除了 now() 函数能获得当前的日期时间外,MySQL 中还有下面的函数:
current_timestamp()
,current_timestamp
,localtime()
,localtime
,localtimestamp    -- (v4.0.6)
,localtimestamp()  -- (v4.0.6)
select sysdate(), sleep(3), sysdate(); -- now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值。
select utc_timestamp(), utc_date(), utc_time(), now() -- 我国位于东八时区,所以本地时间 = UTC 时间 + 8 小时。UTC 时间在业务涉及多个国家和地区的时候,非常有用。

select date_sub(curdate(),interval 1 day); -- 取得前一天
date_sub()函数的例子:
select date_sub(curdate(),interval 1 day) 表示 2013-05-19
select date_sub(curdate(),interval -1 day) 表示 2013-05-21
select date_sub(curdate(),interval 1 month) 表示 2013-04-20
select date_sub(curdate(),interval -1 month) 表示 2013-06-20
select date_sub(curdate(),interval 1 year) 表示 2012-05-20
select date_sub(curdate(),interval -1 year) 表示 2014-05-20


MySQL datediff(date1,date2):两个日期相减 date1 - date2,返回天数。
period_add(P,N):日期加/减去N月。P:“YYYYMM” 或者 “YYMM”,N” :增加或减去 N month(月)。只针对月
period_diff(P1,P2):日期 P1-P2,返回 N 个月。

select datediff('2008-08-08', '2008-08-01'); -- 7
select datediff('2008-08-01', '2008-08-08'); --  -7


1. 选取日期时间的各个部分:日期、时间、年、季度、月、日、小时、分钟、秒、微秒

set @dt = '2008-09-10 07:15:30.123456';

select date(@dt);        -- 2008-09-10
select time(@dt);        -- 07:15:30.123456
select year(@dt);        -- 2008
select quarter(@dt);     -- 3
select month(@dt);       -- 9
select week(@dt);        -- 36
select day(@dt);         -- 10
select hour(@dt);        -- 7
select minute(@dt);      -- 15
select second(@dt);      -- 30
select microsecond(@dt); -- 123456


2. MySQL Extract() 函数,可以上面实现类似的功能:

set @dt = '2008-09-10 07:15:30.123456';

select extract(year                from @dt); -- 2008
select extract(quarter             from @dt); -- 3
select extract(month               from @dt); -- 9
select extract(week                from @dt); -- 36
select extract(day                 from @dt); -- 10
select extract(hour                from @dt); -- 7
select extract(minute              from @dt); -- 15
select extract(second              from @dt); -- 30
select extract(microsecond         from @dt); -- 123456
select extract(year_month          from @dt); -- 200809
select extract(day_hour            from @dt); -- 1007
select extract(day_minute          from @dt); -- 100715
select extract(day_second          from @dt); -- 10071530
select extract(day_microsecond     from @dt); -- 10071530123456
select extract(hour_minute         from @dt); --    715
select extract(hour_second         from @dt); --    71530
select extract(hour_microsecond    from @dt); --    71530123456
select extract(minute_second       from @dt); --      1530
select extract(minute_microsecond  from @dt); --    1530123456
select extract(second_microsecond  from @dt); --     30123456

3. 函数:dayofweek(), dayofmonth(), dayofyear()
分别返回日期参数,在一周、一月、一年中的第几天。

set @dt = '2008-08-08';

select dayofweek(@dt);   -- 6
select dayofmonth(@dt);  -- 8
select dayofyear(@dt);   -- 221

  函数:week(), weekofyear(), dayofweek(), weekday(), yearweek()

set @dt = '2008-08-08';

select week(@dt);        -- 31 位于一年中的第几周
select week(@dt,3);      -- 32 位于一年中的第几周
select weekofyear(@dt);  -- 32 位于一年中的第几周
select dayofweek(@dt);   -- 6 (1 = Sunday, 2 = Monday, ..., 7 = Saturday)
select weekday(@dt);     -- 4 (0 = Monday, 1 = Tuesday, ..., 6 = Sunday)

select yearweek(@dt);    -- 200831

MySQL week() 函数,可以有两个参数,具体可看手册。 weekofyear() 和 week() 一样,都是计算“某天”是位于一年中的第几周。 weekofyear(@dt) 等价于 week(@dt,3)。

MySQL weekday() 函数和 dayofweek() 类似,都是返回“某天”在一周中的位置。不同点在于参考的标准, weekday:(0 = Monday, 1 = Tuesday, ..., 6 = Sunday); dayofweek:(1 = Sunday, 2 = Monday, ..., 7 = Saturday)


4. last_day() 函数:返回月份中的最后一天。

select last_day('2008-02-01');  -- 2008-02-29

5. 函数:dayname(), monthname() 返回星期和月份名称

set @dt = '2008-08-08';

select dayname(@dt);     -- Friday
select monthname(@dt);   -- August

6. date_add() 日期增加一个时间间隔
   date_sub 日期减去一个时间间隔
   
set @dt = now();

select date_add(@dt, interval 1 day);        -- add 1 day
select date_add(@dt, interval 1 hour);       -- add 1 hour
select date_add(@dt, interval 1 minute);     -- ...
select date_add(@dt, interval 1 second);
select date_add(@dt, interval 1 microsecond);
select date_add(@dt, interval 1 week);
select date_add(@dt, interval 1 month);
select date_add(@dt, interval 1 quarter);
select date_add(@dt, interval 1 year);
select date_add(@dt, interval -1 day);       -- sub 1 day select date_sub(@dt, interval 1 day);  

select date_add(@dt, interval '01:15:30' hour_second); --增加了“1小时 15分 30秒
select date_add(@dt, interval '1 01:15:30' day_second); --增加了“1天 1小时 15分 30秒”


1. MySQL (时间、秒)转换函数:time_to_sec(time), sec_to_time(seconds)

select time_to_sec('01:00:05');  -- 3605
select sec_to_time(3605);        -- '01:00:05'

2. MySQL (日期、天数)转换函数:to_days(date), from_days(days)

select to_days('0000-00-00');  -- 0
select to_days('2008-08-08');  -- 733627

select from_days(0);           -- '0000-00-00'
select from_days(733627);      -- '2008-08-08'

3. MySQL Str to Date (字符串转换为日期)函数:str_to_date(str, format)

select str_to_date('08/09/2008', '%m/%d/%Y');                   -- 2008-08-09
select str_to_date('08/09/08'  , '%m/%d/%y');                   -- 2008-08-09
select str_to_date('08.09.2008', '%m.%d.%Y');                   -- 2008-08-09
select str_to_date('08:09:30', '%h:%i:%s');                     -- 08:09:30
select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30

4. MySQL 拼凑日期、时间函数:makdedate(year,dayofyear), maketime(hour,minute,second)

select makedate(2001,31);   -- '2001-01-31'
select makedate(2001,32);   -- '2001-02-01'

select maketime(12,15,30);  -- '12:15:30'