当前位置:网站首页>Important SQL server functions - date functions
Important SQL server functions - date functions
2022-07-28 04:24:00 【Navicat China】
The first 3 part : Date function
stay IT I'm working in the industry 20 Years later, , I can determine the date and time, which is very difficult to deal with . Fortunately, , Modern relational databases ( for example SQL Server) There are many very useful functions for this . In today's article , We will explore some of the most commonly used functions .
Get the current date and time
Every programming language needs a way to get the current date and / Or the method of time . stay SQL Server in , There are two ways to do this CURRENT_TIMESTAMP and GETDATE() Function to get the current date and time . Both use “YYYY-MM-DD hh:mm:ss.mmm” Format returns the current date and time :

that , Why use these two functions ? As you can see in the screenshot above ,GETDATE() Need brackets , and CURRENT_TIMESTAMP You don't need brackets . This makes it very suitable for setting the default value of the audit field , For example, the creation date and last modification date columns :

DATEPART
Being able to get the current date and time is one thing , But sometimes you need to parse each date part . This is it. DATEPART() The use of functions . It returns the specified part of the date as an integer value . This is its grammar :
DATEPART(interval, date)
interval( interval ) The parameter must be a specific date part or abbreviation . for example , Year can be expressed as year、yyyy or 、yy . This is the complete list :
- year, yyyy, yy = year
- quarter, qq, q = season
- month, mm, m = month
- dayofyear, dy, y = One day of the year
- day, dd, d = One day of the month
- week, ww, wk = Zhou
- weekday, dw, w = On weekdays
- hour, hh = Hours
- minute, mi, n = branch
- second, ss, s = second
- millisecond, ms = millisecond
The following query divides the current date into days 、 Month and year components :

DATEFROMPARTS
Date or time functions can also help us build dates based on different data . It accepts years 、 Month and day as input parameters , And combine them to form a complete date :
DATEFROMPARTS(year, month, day)
Here is an example :

DATEADD
Adding a date or time interval to a date or subtracting a date or time interval from a date is the most common date operation . stay SQL Server in , The function to do this is DATEADD. It accepts three input parameters : Interval to add , Quantity and the date to which the interval is applied :
DATEADD(interval, number, date)
DATEADD The interval between acceptance and DATEPART At the same interval , We have said before , Therefore, I will not repeat . Instead, , Let's take a look at some examples of this important function .
Our first example adds three months to today's date :

To subtract the interval , Just provide a negative parameter :

DATEDIFF
Our last function returns the difference between two date values , Expressed in the interval provided ( See above , For a complete list of accepted values ):
DATEDIFF(interval, date1, date2)
The following query returns the difference between two dates in months :

The first date is usually regarded as the earlier date , therefore , If the second date parameter is before the first date , be DATEDIFF The result is expressed as a negative value :

summary
In today's article , We introduced some of the most important SQL Server Date and time functions . In the next issue ( It's also the last issue ) in , We will study other functions . If you are right about Navicat for SQL Server Interested in , Free trial 14 God !
Looking back
Navicat Poisoned | The truth is coming !
Equipment paralysis caused by piracy
Navicat 16.1 by OceanBase Community Edition
Navicat Become a member of the database Innovation Laboratory of the Academy of communications
Navicat Academic partnership program - Free education application
Navicat Technology think tank - Practical exercises and answers to various hot questions
Free trial introduction | Navciat 16 Database management tools
边栏推荐
- Network visualization: features of convolution kernel and CNN visualization (through the attention part of gradient visualization network)
- Cookies and session
- RN interface jump description
- Kotlin——函数
- idea2022更改本地仓库,配置阿里云中央仓库
- Fedformer MOE module
- [record of question brushing] 9. Number of palindromes
- Detailed explanation of pl/sql parameters ("box model")
- 金仓数据库KingbaseES安全指南--4 数据访问保护
- 重要的 SQL Server 函数 - 日期函数
猜你喜欢

Detailed explanation of pl/sql parameters ("box model")

一名合格的软件测试工程师,应该具备哪些技术能力?

Idea start project MVN command terminal cannot recognize "MVN" item as cmdlet

《Intel Arria 10 Avalon-MM DMA Interface for PCI Express Solutions User Guide》文档学习

UBI read only file system

Adding DSP library to STM32F103 and its solution
![[day03] process control statement](/img/4d/d66140962b7e121a2fea2c366a972a.png)
[day03] process control statement

Information system project manager (2022) - key content: Project Contract Management (13)

Information system project manager (2022) - key content: Project Portfolio Management (19)

H. 265 web player easyplayer realizes webrtc video real-time recording function
随机推荐
Full resolution of the use of go native plug-ins
Information system project manager (2022) - key content: Project Risk Management (11)
[record of question brushing] 9. Number of palindromes
IPC: multi process binder Aidl user guide, including cases
仿真测试断开服务器公网连接
Esp8266 WiFi module and mobile communication
idea启动项目mvn命令终端用不了法将“mvn”项识别为 cmdlet
Virtual machine class loading mechanism
pl/sql之各参数详解(“箱子模型“)
H265/HEVC名词解释-- CTU,CTB,CU,CB,TU,PU,TB,PB,LCU,Slice,Tile,Chroma,Luma,I帧,B帧,P帧
VAE generation model (with VAE implementation MNIST code)
Filters, interceptors, listeners
功耗:Leakage Power
Network visualization: features of convolution kernel and CNN visualization (through the attention part of gradient visualization network)
Domestic high hidden free agent crawler code
ftp服务器、nfs服务器的搭建和使用
RN interface jump description
servlet使用
un7.27:如何在idea中成功搭建若依框架项目?
10 more advanced open source command line tools