当前位置:网站首页>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-28 21:32:00 【Game programming】
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 StudentsWHERE 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 ~
author : Meow Ningyi
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- Leetcode daily question - 30 Concatenate substrings of all words
- Leetcode daily question - 522 Longest special sequence II
- How do I download videos? Look at the super simple method!
- 什么是接口?什么是接口测试?
- go-cryptobin 常用加密解密库
- Flask——总结
- 【读书会第13期】视频文件的封装格式
- Apisik helps Middle East social software realize localized deployment
- Real time transformer: meituan's research on single image depth estimation
- Anr analysis - question 1
猜你喜欢

Smarca2 antibody study: abnova smarca2 monoclonal antibody protocol

视频号如何下载视频?来看超简单方法!

接口用例设计

The rogue downloader named by 315 is back

How to analyze the relationship between enterprise digital transformation and data asset management?

力扣树的进一步应用

Pyechart drawing multiple Y-axis line graphs

Interface test process

图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer

LeetCode每日一题——515. 在每个树行中找最大值
随机推荐
Bitbucket failed to pull the warehouse Using SSH
图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer
Query rewriting for opengauss kernel analysis
力扣树的进一步应用
RT thread thread synchronization and thread communication
Leetcode: merge K ascending linked lists_ twenty-three
pyechart绘制多条y轴折线图
Bitbucket failed to pull the warehouse Using SSH
16 `bs对象.节点名div.属性contents` children descendants 获取子节点 子孙节点
User network model and QoE
The blocks problem (uva101) Purple Book p110vector application
Real time transformer: meituan's research on single image depth estimation
Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication
rapid ssl通配符证书八百一年是正版吗
LeetCode123. The best time to buy and sell stocks III
Application of the purple book p113map of ananagrams (uva156)
Explanation of memory dump triggered by software watchdog and anr
LeetCode122. 买卖股票的最佳时机II
Leetcode: expand a binary tree into a linked list_ one hundred and fourteen
LeetCode每日一题——30. 串联所有单词的子串