当前位置:网站首页>MySQL date function

MySQL date function

2022-07-06 17:01:00 My71

The database used in the following , May refer to Create exercise database

Return current date

  1. function :CURDATE()

  2. Instructions

    SELECT CURDATE();
    
  3. Running results

Return current time

  1. function :CURTIME()

  2. Instructions

    SELECT CURTIME();
    
  3. Running results

Returns the current date and time

  1. function :NOW()

  2. Instructions

    SELECT NOW()
    
  3. Running results

Extract date value

  1. function :DATE()

  2. Instructions

    SELECT DATE('2021-10-31 11:36:20')
    
  3. Running results

Extract the year value

  1. function :YEAR()

  2. Instructions

    SELECT YEAR('2021-10-31 11:36:20')
    
  3. Running results

Extract the month value

  1. function :MONTH()

  2. Instructions

    SELECT MONTH('2021-10-31 11:36:20')
    
  3. Running results

Extract the day value

  1. function :DAY()

  2. Instructions

    SELECT DAY('2021-10-31 11:36:20')
    
  3. Running results

Extract the hour value

  1. function :HOUR()

  2. Instructions

    SELECT HOUR('2021-10-31 11:36:20')
    
  3. Running results

Extract the minute value

  1. function :MINUTE()

  2. Instructions

    SELECT MINUTE('2021-10-31 11:36:20')
    
  3. Running results

Extract second value

  1. function :SECOND()

  2. Instructions

    SELECT SECOND('2021-10-31 11:36:20')
    
  3. Running results

Convert seconds into time

  1. function :SEC_TO_TIME

  2. Instructions

    SELECT SEC_TO_TIME(3661)
    
  3. Running results

Turn the time into seconds

  1. function :TIME_TO_SEC()

  2. Instructions

    SELECT TIME_TO_SEC('1:01:01')
    
  3. Running results

Calculation n Days after

  1. function :ADDDATE(d,n)

  2. explain :d Indicates the date ,n Days .

  3. Instructions

    SELECT ADDDATE('2021-10-31',10)
    
  4. Running results

Calculation n The day before

  1. function :SUBDATE(d,n)

  2. explain :d Indicates the date ,n Days .

  3. Instructions

    SELECT SUBDATE('2021-10-31 12:23:20',10)
    
  4. Running results

Calculation n Time in seconds

  1. function :ADDTIME(t,n)

  2. explain :t Time ,n Indicates the time interval , When n When it is an integer, it should be less than 60, It can also be expressed as a string .

  3. Instructions

    SELECT ADDTIME('2021-10-31 11:43:25',5),ADDTIME('2021-10-31 11:43:25','2:10:15')
    
  4. Running results

Calculation n Seconds ago

  1. function :SUBTIME()

  2. explain :t Time ,n Indicates the time interval , When n When it is an integer, it should be less than 60, It can also be expressed as a string .

  3. Instructions

    SELECT SUBTIME('2021-10-31 12:23:20',10),SUBTIME('2021-10-31 12:23:20','2:3:20')
    
  4. Running results

Calculate the date interval

  1. function :DATEDIFF(d1,d2)

  2. explain :d1 End date ,d2 Start date .

  3. Instructions

    SELECT DATEDIFF('2022-01-01','2021-10-31'),DATEDIFF('2021-10-31','2022-01-01')
    
  4. Running results

Calculate the time interval

  1. function :TIMEDIFF(t1,t2)

  2. explain :t1 It's the start time t2 End time

  3. Instructions

    SELECT TIMEDIFF('12:31:20','11:30:10')
    
  4. Running results

practice : Calculation xs The ages of all the people in the table

  1. Instructions

    select  Student number , full name , Date of birth ,DATEDIFF(NOW(), Date of birth ) DIV 365 as  Age  from xs;
    
  2. Running results

原网站

版权声明
本文为[My71]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060922439617.html