MySQL 中常用的日期相關函式

2021-10-19 12:06:56 字數 3963 閱讀 7527

第二部分 日期、時間戳、字串互相轉換

語法:timestampdiff(interval, datetime1,datetime2)

結果:返回(時間2-時間1)的時間差,結果單位由 interval 引數給出。

interval

frac_second 毫秒(低版本不支援,用second,再除於1000)

second 秒

minute 分鐘

hour 小時

day 天

week 周

month 月

quarter 季度

year 年

注意:mysql 5.6之後才支援毫秒的記錄和計算,如果是之前的版本,最好是在資料庫除datetime型別之外的字段,再建立用於儲存毫秒的int欄位,然後自己進行轉換計算。# 所有格式

select timestampdiff(frac_second,

'2012-10-01'

,'2013-01-13');

# 暫不支援

select timestampdiff(

second

,'2012-10-01'

,'2013-01-13');

# 8985600

select timestampdiff(

minute

,'2012-10-01'

,'2013-01-13');

# 149760

select timestampdiff(

hour

,'2012-10-01'

,'2013-01-13');

# 2496

select timestampdiff(

day,

'2012-10-01'

,'2013-01-13');

# 104

select timestampdiff(week,

'2012-10-01'

,'2013-01-13');

# 14

select timestampdiff(

month

,'2012-10-01'

,'2013-01-13');

# 3select timestampdiff(quarter,

'2012-10-01'

,'2013-01-13');

# 1select timestampdiff(

year

,'2012-10-01'

,'2013-01-13');

# 0

語法:select datediff(日期1, 日期2)

結果:日期1 - 日期2 的天數的差

slect datediff(

'2013-01-13'

,'2012-10-01');

# 104

語法:timediff(time1,time2)

結果:返回 time1-time2 的差值

select timediff(

'2018-05-21 14:51:43'

,'2018-05-19 12:54:43');

#49:57:00

注意:該方法兩個引數必須為日期格式

select

now();

# 2018-05-21 14:41:00

select curdate();

# 2018-05-21

select curtime();

# 14:41:38

select

date

(now()

);# 2018-05-21

select sysdate();

# 2018-05-21 14:47:11

select

current_time()

;# 14:51:30

select

current_timestamp

;# 2018-05-21 14:51:37

select

current_timestamp()

;# 2018-05-21 14:51:43

注意:now()與sysdate()類似,只不過now()在執行開始時就獲取,而sysdate()可以在函式執行時動態獲取。

#時間日期轉字串  相當與oracle中的to_char函式

select date_format(

now(),

'%y-%m-%d');

#結果:2017-02-27

#時間轉時間戳

select unix_timestamp(

now())

;#結果:1488160428

#字串轉時間

select str_to_date(

'2017-02-27'

,'%y-%m-%d %h');

#結果:2017-02-27 00:00:00

select str_to_date(

'2017-10-16 15:30:28'

,'%y-%m-%d %h:%i:%s');

#結果 2017-10-16 15:30:28

#注意 年是大寫『y』,小時也必須是大寫『h』 (如果其他為大寫,則得到結果為null)

#字串轉時間戳

select unix_timestamp(

'2017-02-27');

#結果:1488124800

#時間戳轉時間

select from_unixtime(

1488160428);

#結果:2017-02-27 09:53:48

#時間戳轉字串

select from_unixtime(

1488160428

,'%y-%m-%d %t');

#結果:2017-02-27 09:53:48

select date_format(curdate(),

'%y-%m-%d %h:%i:%s');

-- 獲取當天零點

select curdate();

--獲取當前日期

select last_day(curdate())

;--獲取當月最後一天。

select date_add(curdate(),

interval

-day

(curdate())

+1day)

;--獲取本月第一天

select date_add(curdate()-

day(curdate())

+1,interval

1month);

-- 獲取下個月的第一天

select datediff(date_add(curdate()-

day(curdate())

+1,interval

1month

),date_add(curdate(),

interval

-day

(curdate())

+1day)

)from dual;

--獲取當前月的天數

select

day(

'2021-2-17'

)-- 當前月的第幾天(17)

select

month

('2021-2-17'

)-- 當前為幾月(2)

Mysql中常用的日期和時間函式

乾貨 返回日期date的星期索引 1 星期天,2 星期一,7 星期六 dayofweek date select dayofweek 2019 07 17 4 星期三 select dayofweek now 5 星期四 返回date的星期索引 0 星期一,1 星期二,6 星期天 weekday d...

SQL中常用的日期函式

1.getdate 返回當前系統日期 select getdate 2021 03 19 18 30 33.563 2.dateadd 日期部分,常數,日期 返回將日期的指定日期部分加常數後的結果 datesub 日期部分,常數,日期 返回將日期的指定日期部分減去常數後的結果 日期部分可以寫成 等同...

MySQL日期相關函式

1.獲取當前日期 curdate current date current date mysql select curdate mysql select curtime 另外獲取utc 全球吧標準時間,原先也被稱作格林威治標準時間或gmt 時間,本地時間 utc 時間 8 小時,相應的函式是 utc...