Table of Contents
ToggleThe Date & Time functions are built-in functions in the SQL Server that allow you to manipulate and perform operations on date and time values in the SQL Server.
These functions can be used in SQL Server queries to perform various date and time operations, such as filtering records based on dates, calculating date differences, and formatting dates for display purposes.
Below is a comprehensive list of SQL Server Date & Time functions along with their descriptions:
| Sr.No. | Function | Description |
|---|---|---|
| 1 | @@DATEFIRST |
This function is used to retrieve the first day of the week which is set by the SET DATEFIRST function. |
| 2 | CURRENT_TIMESTAMP |
Is used to retrieve the current date and time. |
| 3 | CURRENT_TIMEZONE() |
This function is used to retrieve the current time zone offset from Coordinated Universal Time (UTC). |
| 4 | CURRENT_TIMEZONE_ID() |
Is used to retrieve the current time zone ID observed by a server or an instance. |
| 5 | DATE_BUCKET() |
Is used to group data into groups that correspond to fixed periods of time. |
| 6 | DATEADD() |
Is used to add a specific number of intervals to a given date or time value. |
| 7 | DATEDIFF() |
Is used to calculate the difference between two date values and returns in a int data type. |
| 8 | DATEDIFF_BIG() |
Is used to calculate the difference between two dates values and return in a bigint data type. |
| 9 | DATEFROMPARTS() |
This function is used to retrieve a date from individual segments such as year, month, and day. |
| 10 | DATENAME() |
This function is used to retrieve a specified part of a date or time value as string. |
| 11 | DATEPART() |
Is used to return a specified part of a date or time value as integer. |
| 12 | DATETIME2FROMPARTS() |
Is used to construct a datetime2 value from an individual date and time segments. |
| 13 | DATETIMEFROMPARTS() |
Is used to construct a datetime value from an individual date and time segments. |
| 14 | DATETIMEOFFSETFROMPARTS() |
Is used to extract a datetimeoffset value from each of a date’s component parts. |
| 15 | DATETRUNC() |
Is used to truncate a date or time value to a specified datepart (such as year, month, day, etc.). |
| 16 | DAY() |
This function is used to get the day of the month for a specified date. |
| 17 | EOMONTH() |
This function is used to get the last day of the month for a specified date value. |
| 18 | GETDATE() |
This function is used to get the current database system date and time. |
| 19 | GETUTCDATE() |
This function is used to get the current database system UTC date and time. |
| 20 | ISDATE() |
Is used to determine whether a value is a valid date or not. |
| 21 | MONTH() |
This function is used to get the month part for a specified date (value can be from 1 to 12). |
| 22 | SMALLDATETIMEFROMPARTS() |
Is used to construct a new datetime value from individual segments (such as year, month, etc.). |
| 23 | SWITCHOFFSET() |
This function is used to get datetimeoffset values that have been changed from the stored time zone offset to a given new time zone offset. |
| 24 | SYSDATETIME() |
This function is used to get the date and time of the SQL Server. |
| 25 | SYSDATETIMEOFFSET() |
This function is used to get a value of DATETIMEOFFSET(7) that provides the current system date and time and also timezone of the SQL Server. |
| 26 | SYSUTCDATETIME() |
This function is used to get date and time of the computer on which the instance of SQL Server is running. |
| 27 | TIMEFROMPARTS() |
Is used to construct a time value from individual segments. |
| 28 | TODATETIMEOFFSET() |
This function is used to get a datetimeoffset value that is translated from a datetime2 expression. |
| 29 | YEAR() |
This function is used to get an integer number that represents the year of the given date. |
Let’s see a practical example that combines some of these functions to perform a real-world task, such as calculating the age of employees based on their birth dates and formatting the output.
-- Example: Calculate employee age and format their birth date
SELECT
EmployeeName,
BirthDate,
DATENAME(WEEKDAY, BirthDate) AS DayOfBirth,
DATEDIFF(YEAR, BirthDate, GETDATE()) AS Age,
EOMONTH(BirthDate) AS EndOfBirthMonth
FROM Employees
WHERE YEAR(BirthDate) > 1980;
Explanation:
– DATENAME(WEEKDAY, BirthDate): Extracts the day of the week (e.g., “Monday”) of the birth date.
– DATEDIFF(YEAR, BirthDate, GETDATE()): Calculates the age by finding the year difference between the birth date and current date.
– EOMONTH(BirthDate): Returns the last day of the birth month.
– YEAR(BirthDate): Filters employees born after 1980.
Key Takeaway: Master SQL Server Date & Time functions with Vista Academy!
