• Latest
  • Trending
Use of Date and Time Functions in SQL Server

Use of Date and Time Functions in SQL Server

June 22, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Thursday, 16 July, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

Use of Date and Time Functions in SQL Server

by ITECHNEWS
June 22, 2022
in Data Science, Leading Stories
0 0
0
Use of Date and Time Functions in SQL Server

Introduction on SQL Server

Before going to our main concept, we must first learn about what are functions and then study the details of scalar functions in SQL Server.

Function in SQL Server is a set of T-SQL statements that are used to perform a specific operation. SQL Server provides a variety of functions such as analytic functions, scalar functions, aggregate functions, etc.

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

Scalar functions are used in SQL Server when we want to operate on a single value. SQL Server scalar functions can take one or more parameters as input and always return a single value. The use of scalar functions helps to simplify data querying in SQL Server. There are various categories of scalar functions such as JSON functions, cursor functions, date and time functions, logical functions, etc. depending upon the operations performed by them.

In this article, we will study a category of scalar functions- Date and time functions. Date and time functions are used to perform operations on date and time input values and return a string, numeric, or date and time values.

Date and time functions are used to perform specific types of operations such as returning the current date and time, modifying date and time values, returning differences between two dates, validating date and time values, etc.

Let’s discuss the types of date and time functions available in SQL Server-

SQL Server Date and Time Functions

SQL Server provides several types of date and time functions such as SYSUTCDATETIME(), CURRENT_TIMESTAMP, GETDATE(), DAY(), MONTH(), YEAR(), DATEFROMPARTS (), DATETIME2FROMPARTS(), TIMEFROMPARTS (), DATEDIFF(), DATEADD(), ISDATE(), etc. This functions are used to perform operations on date and time input.

SYSUTCDATETIME(): This function is used to return the date and time values of the computer on which the instance of SQL Server is running in the UTC format. The function returns datetime2(7) value as the result.

Syntax: SYSUTCDATETIME()
Example: SELECT SYSUTCDATETIME() AS [SYSUTCDATETIME()]
SQL Server Date and Time Functions

CURRENT_TIMESTAMP: This function is used to return the datetime value containing the date and time values of the computer on which the instance of SQL Server is running. The function returns datetime value as the result, excluding the time zone offset.

Syntax: CURRENT_TIMESTAMP
Example: SELECT CURRENT_TIMESTAMP AS [CURRENT_TIMESTAMP]
SQL Server Date and Time Functions Image 2

GETDATE(): This function is used to return the datetime value containing the date and time values of the computer on which the instance of SQL Server is running. The function returns datetime value as the result, excluding the time zone offset. The difference between CURRENT_TMESTAMP and GETDATE() is that CURRENT_TIMESTAMP is an ANSI SQL function whereas GETDATE is the T-SQL version of that same function.

In most scenarios, CURRENT_TIMESTAMP is recommended to use as it is portable to any ANSI-compliant database, whereas GETDATE() is not.

Syntax: GETDATE()
Example: SELECT GETDATE() AS [GETDATE()]
SQL Server Date and Time Functions

DAY(): This function is used to return an integer which represents the day (day of the month) part of the specified date. The DAY function takes date as an argument. The provided argument can be either of the types datetime, date, datetime2, time and small datetime. Users can pass a column expression, expression, string literal or user-defined variable as an argument to the DAY function.

Note: If the provided date argument only contains a time part, then the DAY function will return 1 – the base day as result.

Syntax: DAY(date)
Example 1: SELECT DAY('2022-09-02') AS [DAY]
Example 2: SELECT DAY('2022-09-12  01:01:01.2234567') AS [DAY]
SQL Server Date and Time Functions Image 4
SQL Server Date and Time Functions Image 5

MONTH(): This function is used to return an integer which represents the month part of the specified date. The MONTH function takes date as an argument. The provided argument can be either of the types datetimeoffset, time, date, datetime, smalldatetime or datetime2. Users can pass a column expression, expression, string literal or user-defined variable as an argument to the MONTH function.

Note: If the provided date argument only contains a time part, then the MONTH function will return 1, the base month.

Syntax: MONTH(date)
Example 1: SELECT MONTH ('2022-10-02') AS [MONTH]
Example 2: SELECT MONTH ('2022-01-12  01:01:01.2234567') AS [MONTH]

 

SQL Server Date and Time Functions Image 6
SQL Server Date and Time Functions Image 7

YEAR(): This function is used to return an integer which represents the year part of the specified date. The YEAR function takes date as an argument. The provided argument can be either of the types datetimeoffset, time, date, datetime, smalldatetime or datetime2. Users can pass a column expression, expression, string literal or user-defined variable as an argument to the YEAR function.

Note: If the provided date argument only contains a time part, then the YEAR function will return 1900, the base year.

Syntax: YEAR(date)
Example 1: SELECT YEAR ('1998-10-02') AS [YEAR]
Example 2: SELECT YEAR('2022-01-12  01:01:01.2234567') AS [YEAR]
SQL Server Date and Time Functions Image 8
SQL Server Date and Time Functions Image 9

DATEFROMPARTS(): This function is used to return date for the specified year, month, and day. The DATEFROMPARTS function takes year (specifies the year), month (specifies the month) and day (specifies the day) as the required arguments. If an invalid year, month or day is provided, then DATEFROMPARTS function will raise an error. Null is returned from DATEFROMPARTS function if at least one required argument has a null value.

Syntax: DATEFROMPARTS(year, month, day)
Example: SELECT DATEFROMPARTS ( 2012, 09, 15 ) AS Result;
SQL Server Date and Time Functions Image 10

DATETIME2FROMPARTS(): This function is used to return date for the specified date and time, with the specified precision. The DATETIME2FROMPARTS function takes year (specifies the year), month (specifies the month), day (specifies the day), hour (specifies the hours), minute (specifies the minutes), seconds (specifies the seconds), fractions (specifies the fractional seconds value) and precision (specifies the precision of the datetime2 value returned by the DATETIME2FROMPARTS function) as the required arguments. If an invalid required argument is provided, then DATETIME2FROMPARTS function will raise an error. Null is returned from DATETIME2FROMPARTS function if at least one required argument has a null value. However, if the precision argument has a null value, an error is raised by the DATETIME2FROMPARTS function.

Syntax: DATETIME2FROMPARTS (year, month, day, hour, minute, seconds, fractions, precision)
Example: SELECT DATETIME2FROMPARTS ( 2022, 09, 15, 12, 34, 56, 7,2 ) AS Result;

 

SQL Server Date and Time Functions Image 11

DATEDIFF(): This function is used to return the count of the specified datepart boundaries, crossed between two specified dates. The DATEDIFF function takes datepart (specifies the units in which DATEDIFF reports the difference between the startdate and enddate), startdate and enddate as arguments. The startdate and enddate can be either of the type datetime, date, datetime2, datetimeoffset, time and smalldatetime.

Syntax: DATEDIFF(datepart, startdate, endate)
Example: SELECT DATEDIFF(MONTH, '2010-08-01', '2022-02-28');
SQL Server Date and Time Functions Image 12

Conclusion

The date and time functions are one of the most important categories in scalar functions in SQL Server. To summarize, the following were the major takeaways about the various date and time functions:

  1. We learned how to find the current date and time of your system in UTC format.
  2. We have also seen how CURRENT_TMESTAMP is different from GETDATE().
  3. We also learned how to extract the day, month, and year from the given date.
  4. We understood how we can use DATEDIFF() to find the differences between the two dates.
  5. Apart from this, we also learned how to construct dates from the month, year, day, and time details.
Source: Chaitanya Shah
Via: analyticsvidhya
Tags: Use of Date and Time Functions in SQL Server
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version