Zero-based self-study SQL course | Complete collection of date functions in SQL

Hello everyone, my name is Ning Yi.

Today is our lesson 21: Date functions in SQL.

The built-in date functions in MySQL include obtaining date, date formatting, and date calculation. Let’s take a look at them separately.

1. Get date function

MySQL’s built-in date and time function:

SELECT  
  NOW() AS '当前日期+时间',
  CURDATE() AS '当前日期',
  CURTIME() AS '当前时间';

Get the specific year, month and day function:

SELECT  
  YEAR("2022-04-11 15:44:28") AS '年',
  MONTH("2022-04-11 15:44:28") AS '月',
  DAY("2022-04-11 15:44:28") AS '日',
  HOUR("2022-04-11 15:44:28") AS '小时',
  MINUTE("2022-04-11 15:44:28") AS '分钟',
  SECOND("2022-04-11 15:44:28") AS '秒',
  DAYNAME("2022-04-11 15:44:28") AS '星期几',
  MONTHNAME("2022-04-11 15:44:28") AS '几月';

Example: In the Students table, find student records whose birthday is 1995.

SELECT *
FROM Students
WHERE YEAR(Sage) = 1995

We have done this question before, using BETWEEN…AND…. You can click on the homepage to find the fifth lecture - review of WHERE conditional clauses~

2. Format date function

We mainly use the DATE_FORMAT and TIME_FORMAT functions to format dates and times. Let’s take a look at the specific usage.

SELECT
  NOW() AS "现在时间",
  DATE_FORMAT(NOW(), '%Y.%m.%d') AS '格式化日期',
  TIME_FORMAT(NOW(), '%h:%i:%s') AS '格式化时间'

Available formats are:

3. Calculate date function

In actual business, we often need to calculate dates and times, such as adding or subtracting one day from a date, or calculating date intervals. Let’s take a look at the specific usage.

Add or subtract days from a date:

SELECT
 NOW() AS "现在时间",
 DATE_ADD(NOW(), INTERVAL 1 DAY) AS "增加1天",
 DATE_SUB(NOW(), INTERVAL 1 DAY) AS "减少1天"

You can also add or subtract years, months, hours, and minutes:

SELECT
  NOW() AS "现在时间",
  DATE_ADD(NOW(), INTERVAL 1 YEAR) AS "增加1年",
  DATE_SUB(NOW(), INTERVAL 1 MONTH) AS "减少1天",
  DATE_SUB(NOW(), INTERVAL 1 HOUR) AS "减少1小时"

Calculate the number of days between two dates:

SELECT
  DATEDIFF('2022-04-11','2021-04-11') AS "间隔天数",
  DATEDIFF('2022-04-11 01:00','2022-04-10 23:00') AS "间隔天数"

Note that the DATEDIFF function ignores the time part and only calculates the date difference. For example, in the above SQL statement, there is only a two-hour difference between 04-10 23:00 and 04-11 01:00, but using the DATEDIFF function will calculate the time interval as 1 day. .

Homework: In the Students table, calculate the age of each student through the student's birthday Sage, and finally display three columns: today's date, student name, and student age.

Assignment analysis: Use the DATEDIFF function to calculate the number of days between today and the student's birthday Sage, divide by 365 to get the number of years, and then use the FLOOR numerical function to get a large integer smaller than the age.

SELECT
  DATE_FORMAT(NOW(),'%Y-%m-%d') AS '今天日期',
  Sname AS '学生姓名',
  FLOOR(DATEDIFF(NOW(),Sage)/365) AS "学生年龄"
FROM Students;

It should be noted that the above functions are not standard SQL statements, but functions built into the MySQL software, and may not be universal in Oracle or SQL Server.

In the next lesson we will talk about the IF function.

Click to follow and update the course as soon as possible~

Guess you like

Origin blog.csdn.net/shine_a/article/details/125460200