Detailed explanation of single-row function code of date type in MySQL

Detailed explanation of single-row function code of date type in MySQL

Date-type single-row functions in MySQL:

CURDATE() or CURRENT_DATE() returns the current date (accurate to the day)
The reference code is as follows:

SELECT CURDATE()
FROM DUAL;
SELECT CURRENT_DATE()
FROM DUAL;

-----------------------------------(dividing line)------------------------------------

CURTIME() or CURRENT_TIME() returns the current time (accurate to seconds)
The reference code is as follows:

SELECT CURTIME()
FROM DUAL;
SELECT CURRENT_TIME()
FROM DUAL;

-----------------------------------(dividing line)------------------------------------

NOW() / SYSDATE() / CURRENT_TIMESTAMP() / LOCALTIME() / LOCALTIMESTAMP() returns the current system date and time (accurate to seconds)
The reference code is as follows:

SELECT NOW()
FROM DUAL;
SELECT LOCALTIME()
FROM DUAL;

-----------------------------------(dividing line)------------------------------------

YEAR(date) displays year / MONTH(date) displays month / DAY(date) displays day / HOUR(time) displays hour / MINUTE(TIME) displays minute / SECOND(TIME) displays second Reference code is as follows:

SELECT YEAR(CURDATE()), MONTH(CURDATE()), DAY(CURDATE())
FROM DUAL;
SELECT HOUR(CURTIME()),MINUTE(NOW()),SECOND(NOW())
FROM DUAL;

Note: The functions here, and the contents in the brackets come from the functions mentioned above.

-----------------------------------(dividing line)------------------------------------

WEEK(date) / WEEKOFYEAR(date) returns the first week of the year. The reference code is as follows:

SELECT WEEK(NOW())
FROM DUAL;
SELECT WEEKOFYEAR(CURDATE())
FROM DUAL;

-----------------------------------(dividing line)------------------------------------

DAYOFWEEK(DATE) returns the day of the week. Note: Sunday is 1, Monday is 2, ... Saturday is 7
The reference code is as follows:

SELECT DAYOFWEEK(NOW())  
FROM DUAL;

The code compilation results are as follows:

insert image description here

Explain that it is Thursday

-----------------------------------(dividing line)------------------------------------

WEEKDAY(DATE) returns the day of the week. Note that Monday is 0, Tuesday is 1, ... Sunday is 6
The reference code is as follows:

SELECT WEEKDAY(NOW())  
FROM DUAL;

The code compilation results are as follows:

insert image description here

Explain that it is Thursday

-----------------------------------(dividing line)------------------------------------

8. DAYNAME(date) returns the day of the week: MONDAY, TUESDAY…SUNDAY
The reference code is as follows:

SELECT DAYNAME(CURDATE()),DAYNAME('2020-09-06') #implicit conversion FROM DUAL;

The code compilation results are as follows:

insert image description here

Remark:
A conversion is used here, and the date can be converted to a string by default, which is called implicit conversion. In addition, a few common concepts are added:
Formatting: Converting a date to a string Parsing: Converting a string to a date Example code is as follows:

Convert string to date (parse) (explicit conversion)

SELECT STR_TO_DATE('09/01/2009','%m/%d/%Y')
FROM DUAL;

The code compilation results are as follows:

insert image description here

Convert string to date (parse) (explicit conversion)

SELECT STR_TO_DATE('20140422154706','%Y%m%d%H%i%s')
FROM DUAL;

The code compilation results are as follows:

insert image description here

Convert string to date (parse) (explicit conversion)

SELECT STR_TO_DATE('2014-04-22 15:47:06','%Y-%m-%d %H:%i:%s')
FROM DUAL;

The code compilation results are as follows:

insert image description here

-----------------------------------(dividing line)------------------------------------

9. MONTHNAME(DATE) returns the month: January…

SELECT MONTHNAME(NOW())  
FROM DUAL;

-----------------------------------(dividing line)------------------------------------

DATEDIFF(date1,date2) returns the date interval between date1 and date2.
TIMEDIFF(time1,time2) returns the time interval of time1 - time2. The reference code is as follows:

SELECT DATEDIFF('2021-03-06','2021-06-09')  
FROM DUAL;

Note: The returned value is the number of days between the two dates = date1-date2. If date1 is before date2, the returned value is a negative value.
The code compilation results are as follows:

insert image description here

SELECT TIMEDIFF('2019-06-06 18:23:06','2019-08-06 10:36:45')
FROM DUAL;

Note: The returned value is the difference between the two times [hours: minutes: seconds] = time1-time2. If time1 is before time2, the value returned is a negative value.
The code compilation results are as follows:

insert image description here

The above is the details of the date type single-row function in MySQL. For more information about MySQL single-row functions, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Mysql date formatting and complex date range query
  • MySQL method of generating random numbers, strings, dates, verification codes and UUIDs
  • The pitfall of MySQL numeric type auto-increment
  • Analysis of MySQL's problem with sorting numbers in strings
  • MySQL example to explain single-row functions and character math date process control

<<:  Example code of javascript select all/unselect all operation in html

>>:  Two ways to implement text stroke in CSS3 (summary)

Recommend

Introduction to Javascript DOM, nodes and element acquisition

Table of contents DOM node Element node: Text nod...

Summary of MySQL database and table sharding

During project development, our database data is ...

Detailed explanation of Mysql function call optimization

Table of contents Function call optimization Func...

Detailed explanation of Linux curl form login or submission and cookie usage

Preface This article mainly explains how to imple...

A detailed guide to custom directives in Vue

Table of contents 1. What is a custom instruction...

CentOS6.8 uses cmake to install MySQL5.7.18

Referring to the online information, I used cmake...

How does Vue implement communication between components?

Table of contents 1. Communication between father...

Mini Program to Implement the Complete Shopping Cart

The mini program implements a complete shopping c...

Detailed explanation of the correct use of the count function in MySQL

1. Description In MySQL, when we need to get the ...

Vue implements a simple timer component

When doing a project, it is inevitable to encount...

Two ways to enable firewall in Linux service

There are two ways: 1. Service method Check the f...

Example code for implementing complex table headers in html table

Use HTML to create complex tables. Complex tables...