当前位置:网站首页>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 ~
边栏推荐
- vivado VIO IP的用法
- 小程序referer
- [tinyriscv verilator] branch transplanted to Da Vinci development board of punctual atom
- 【剑指Offer】47. 礼物的最大价值
- 量化交易入门教程
- CUDA error:out of memory caused by insufficient video memory of 6G graphics card
- NDSS 2022 接收的列表
- webService
- 【tinyriscv verilator】分支移植到正点原子达芬奇开发板
- [try to hack] kill evaluation
猜你喜欢

How to set the enterprise wechat group robots to send messages regularly?

Usage of vivado vio IP

The file or assembly 'cefsharp.core.runtime.dll' or one of its dependencies could not be loaded. Is not a valid Win32 Application. (exception from hresult:0x800700c1)

This kind of people began to be robbed by VC with a monthly salary of 80000 yuan

【IDEA】IDEA 格式化 代码技巧 idea 格式化 会加 <p> 标签

seata

未能加载文件或程序集“CefSharp.Core.Runtime.dll”或它的某一个依赖项。 不是有效的 Win32 应用程序。 (异常来自 HRESULT:0x800700C1)

【数字IC/FPGA】检测最后一个匹配序列的位置

clickonce 部署ClickOnce应用程序时出错-清单中的引用与下载的程序集的标识不匹配

Introduction to quantitative trading
随机推荐
良/恶性乳腺肿瘤预测(逻辑回归分类器)
【数字IC/FPGA】检测最后一个匹配序列的位置
[learn FPGA programming from scratch -48]: Vision - development and application of intelligent sensors
手把手教你移植 tinyriscv 到FPGA上
[electron] basic learning
[electron] basic learning
How to set the enterprise wechat group robots to send messages regularly?
在线JSON转PlainText工具
沉寂了一段时间 ,我又出来啦~
Stream + Nacos
The National University of Singapore 𞓜 uses model free reinforcement learning to evaluate the energy efficiency of the energy efficiency data center
Halcon's region: features of multiple regions (6)
[js]var, let, const
C language - date formatting [easy to understand]
Senior headhunting team manager: interviewed 3000 consultants, summarized and organized 8 commonalities (Mao Sheng)
How to use the apipost script - global variables
Realization of kaggle cat dog recognition by pytorch
Sentinel
【AI应用】NVIDIA GeForce RTX 1080Ti的详情参数
C WinForm reads the resources picture