当前位置:网站首页>Zero foundation self-study SQL course | complete collection of date functions in SQL
Zero foundation self-study SQL course | complete collection of date functions in SQL
2022-06-27 23:41:00 【Meow Ningyi】
Hello everyone , I'm Ning Yi .
Today is our first 21 course :SQL The date function in .
MySQL Built in date function in , Include acquisition date 、 Date formatting 、 Date calculation , Let's have a look at .
1、 Get date function
MySQL Built in date and time acquisition function :
SELECT
NOW() AS ' The current date + Time ',
CURDATE() AS ' The current date ',
CURTIME() AS ' current time ';
Get the specific month, year and day function :
SELECT
YEAR("2022-04-11 15:44:28") AS ' year ',
MONTH("2022-04-11 15:44:28") AS ' month ',
DAY("2022-04-11 15:44:28") AS ' Japan ',
HOUR("2022-04-11 15:44:28") AS ' Hours ',
MINUTE("2022-04-11 15:44:28") AS ' minute ',
SECOND("2022-04-11 15:44:28") AS ' second ',
DAYNAME("2022-04-11 15:44:28") AS ' What day ',
MONTHNAME("2022-04-11 15:44:28") AS ' How many months ';
example : stay Students In the table , Find out birthday Sage by 1995 Student records for .

SELECT *
FROM Students
WHERE YEAR(Sage) = 1995
We have done this before , use BETWEEN…AND… Realized , You can click on the home page to find the fifth lecture --WHERE Review the conditional clause ~
2、 Format date function
We mainly use DATE_FORMAT、TIME_FORMAT Function to format the date and time , Let's take a look at the specific usage .
SELECT
NOW() AS " present time ",
DATE_FORMAT(NOW(), '%Y.%m.%d') AS ' Format date ',
TIME_FORMAT(NOW(), '%h:%i:%s') AS ' Format time '
The available formats are :

3、 Calculate date function
In real business , We often have to calculate the date and time , For example, add or subtract one day from the date , Or calculate the date interval , Let's take a look at the specific usage .
Increase or decrease the number of days based on the date :
SELECT
NOW() AS " present time ",
DATE_ADD(NOW(), INTERVAL 1 DAY) AS " increase 1 God ",
DATE_SUB(NOW(), INTERVAL 1 DAY) AS " Reduce 1 God "
It can also increase the number of teenagers 、 month 、 Hours 、 minute :
SELECT
NOW() AS " present time ",
DATE_ADD(NOW(), INTERVAL 1 YEAR) AS " increase 1 year ",
DATE_SUB(NOW(), INTERVAL 1 MONTH) AS " Reduce 1 God ",
DATE_SUB(NOW(), INTERVAL 1 HOUR) AS " Reduce 1 Hours "
Calculate the number of days between two dates :
SELECT
DATEDIFF('2022-04-11','2021-04-11') AS " Days between ",
DATEDIFF('2022-04-11 01:00','2022-04-10 23:00') AS " Days between "
Be careful DATEDIFF Function ignores the time part , Calculate only date variance , such as , above SQL In the sentence 04-10 23:00 To 04-11 01:00 It was only two hours away , But with DATEDIFF The function calculates a time interval of 1 God .
Homework : stay Students In the table , By student birthday Sage, Calculate the age of each student , Finally show today's date 、 The student's name 、 Student age 3 Column .
Homework analysis : adopt DATEDIFF function , Calculate today and students' birthdays Sage Days of difference , Divide 365 Get years , Re pass FLOOR Numerical function , Take a large integer smaller than age .
SELECT
DATE_FORMAT(NOW(),'%Y-%m-%d') AS ' Today's date ',
Sname AS ' The student's name ',
FLOOR(DATEDIFF(NOW(),Sage)/365) AS " Student age "
FROM Students;
Note that these functions are not standard SQL sentence , It is MySQL Functions built into the software , stay Oracle、 perhaps SQL Server Not necessarily common in .
Next time we will talk about IF function .
Click on Focus on , Update the course and notify the first time ~
边栏推荐
- ClickOnce error deploying ClickOnce application - the reference in the manifest does not match the identity of the downloaded assembly
- 企业架构师面试的100个问题
- Online JSON to plaintext tool
- 【微服务|Sentinel】sentinel数据持久化
- Swing UI container (I)
- webService
- Excel print settings public header
- SQL中IS NOT NULL与!=NULL的区别
- [js]var, let, const
- [network] common request methods
猜你喜欢

Prediction of benign / malignant breast tumors (logistic regression classifier)

To build a "strong core" in Nansha, the first IC Nansha conference was held in Nansha

良/恶性乳腺肿瘤预测(逻辑回归分类器)

golang - new和make的区别

UESTC (shenhengtao team) & JD AI (Mei Tao team) proposed a structured dual stream attention network for video Q & A, with performance SOTA! Better than the method based on dual video representation!

在线JSON转PlainText工具

打造南沙“强芯”,南沙首届IC Nansha大会召开
![[Blue Bridge Cup training 100 questions] scratch digital calculation Blue Bridge Cup competition special prediction programming question collective training simulation exercise question No. 16](/img/7c/d4ea8747ce45fd2eb59a8f968653db.png)
[Blue Bridge Cup training 100 questions] scratch digital calculation Blue Bridge Cup competition special prediction programming question collective training simulation exercise question No. 16

消除el-image图片周围间隙

Swing UI container (I)
随机推荐
上手了一个自然语言模型BLOOM
小芯片chiplet技术杂谈
Applet referer
Discuz small fish game wind shadow legend business gbk+utf8 version template /dz game website template
思源笔记订阅停止直接删云端数据嘛?
clickonce 部署ClickOnce应用程序时出错-清单中的引用与下载的程序集的标识不匹配
单细胞数据清洗的这5个步骤你会做吗?
【PCL自学:Segmentation4】基于Min-Cut点云分割
[Blue Bridge Cup training 100 questions] scratch digital calculation Blue Bridge Cup competition special prediction programming question collective training simulation exercise question No. 16
To build a "strong core" in Nansha, the first IC Nansha conference was held in Nansha
企业架构师面试的100个问题
【蓝桥杯集训100题】scratch数字计算 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第16题
SQL中IS NOT NULL与!=NULL的区别
mysql读写分离配置
How to use the apipost script - global variables
刚开始看英文文献,想问一下各位,最初应该怎么看进去?
Discuz taobaoke website template / Dean taobaoke shopping style commercial version template
Structure de stockage des graphiques
跨系统数据一致性问题解决方案汇总
How to start ID from 1 after MySQL deletes a table