Hello and welcome to our guide on datetime now in SQL Server. In today’s fast-paced world, time management is crucial to the success of any business. As a database professional, you need to have a thorough understanding of how SQL Server handles dates and times. In this article, we will explore all the intricacies of datetime now in SQL Server, covering everything from data types and functions to best practices and FAQs. So, let’s get started!
Data Types for Dates and Times
Before we dive into datetime now in SQL Server, let’s start with a brief overview of the data types used for dates and times in SQL Server. The most commonly used data types are:
Data Type | Description |
---|---|
DATE | Stores a date value without time (year, month, and day) |
TIME | Stores a time value without date (hour, minute, and second) |
DATETIME | Stores both date and time values (year, month, day, hour, minute, and second) |
DATETIME2 | Stores both date and time values with greater precision than DATETIME |
SMALLDATETIME | Stores both date and time values with less precision than DATETIME (minute accuracy instead of second accuracy) |
OFFSETDATETIME | Stores a date and time value with an offset from UTC (Coordinated Universal Time) |
DATE
The DATE data type is used to store a date value without time. It has a range of January 1, 1 CE (Common Era) through December 31, 9999 CE. The syntax for declaring a DATE variable is:
DECLARE @date_var DATE
You can also assign a value to a DATE variable using the ISO format ‘YYYY-MM-DD’ or the ANSI format ‘MMM DD YYYY’ (e.g., ‘Jan 01 2022’). Here is an example:
DECLARE @date_var DATE = '2022-01-01'
You can perform various operations on DATE values, such as adding or subtracting days, months, or years, comparing dates, and converting dates to other data types. We will cover these operations in detail in the following sections.
TIME
The TIME data type is used to store a time value without date. It has a range of 00:00:00.0000000 through 23:59:59.9999999. The syntax for declaring a TIME variable is:
DECLARE @time_var TIME
You can also assign a value to a TIME variable using the ISO format ‘HH:MI:SS.mmm’ or the 24-hour clock format ‘HH:MI:SS’ (e.g., ’23:59:59.999′). Here is an example:
DECLARE @time_var TIME = '23:59:59.999'
You can perform various operations on TIME values, such as adding or subtracting hours, minutes, or seconds, comparing times, and converting times to other data types. We will cover these operations in detail in the following sections.
DATETIME
The DATETIME data type is used to store both date and time values in a single column. It has a range of January 1, 1753, through December 31, 9999, with an accuracy of 3.33 milliseconds. The syntax for declaring a DATETIME variable is:
DECLARE @datetime_var DATETIME
You can also assign a value to a DATETIME variable using the ISO format ‘YYYY-MM-DD HH:MI:SS.mmm’ or the ANSI format ‘MMM DD YYYY HH:MI:SS.mmm AM/PM’ (e.g., ‘Jan 01 2022 12:00:00.000 AM’). Here is an example:
DECLARE @datetime_var DATETIME = '2022-01-01 12:00:00.000'
You can perform various operations on DATETIME values, such as adding or subtracting days, hours, minutes, or seconds, comparing dates and times, and converting dates and times to other data types. We will cover these operations in detail in the following sections.
DATETIME2
The DATETIME2 data type is used to store both date and time values with greater precision than DATETIME. It has a range of January 1, 1 CE through December 31, 9999 CE, with an accuracy of 100 nanoseconds. The syntax for declaring a DATETIME2 variable is:
DECLARE @datetime2_var DATETIME2
You can also assign a value to a DATETIME2 variable using the ISO format ‘YYYY-MM-DD HH:MI:SS.nnnnnnn’ or the ANSI format ‘MMM DD YYYY HH:MI:SS.nnnnnnn AM/PM’ (e.g., ‘Jan 01 2022 12:00:00.0000000 AM’). Here is an example:
DECLARE @datetime2_var DATETIME2 = '2022-01-01 12:00:00.0000000'
You can perform various operations on DATETIME2 values, such as adding or subtracting days, hours, minutes, seconds, or nanoseconds, comparing dates and times, and converting dates and times to other data types. We will cover these operations in detail in the following sections.
SMALLDATETIME
The SMALLDATETIME data type is used to store both date and time values with less precision than DATETIME. It has a range of January 1, 1900, through June 6, 2079, with an accuracy of 1 minute. The syntax for declaring a SMALLDATETIME variable is:
DECLARE @smalldatetime_var SMALLDATETIME
You can also assign a value to a SMALLDATETIME variable using the ISO format ‘YYYY-MM-DD HH:MI:SS’ or the ANSI format ‘MMM DD YYYY HH:MI:SS AM/PM’ (e.g., ‘Jan 01 2022 12:00:00 AM’). Here is an example:
DECLARE @smalldatetime_var SMALLDATETIME = '2022-01-01 12:00:00'
You can perform various operations on SMALLDATETIME values, such as adding or subtracting days, hours, or minutes, comparing dates and times, and converting dates and times to other data types. We will cover these operations in detail in the following sections.
OFFSETDATETIME
The OFFSETDATETIME data type is used to store a date and time value with an offset from UTC (Coordinated Universal Time). It has a range of January 1, 1 CE through December 31, 9999 CE, with an accuracy of 100 nanoseconds. The syntax for declaring an OFFSETDATETIME variable is:
DECLARE @offsetdatetime_var OFFSETDATETIME
You can also assign a value to an OFFSETDATETIME variable using the ISO format ‘YYYY-MM-DD HH:MI:SS.nnnnnnn+/-HH:MI’ (e.g., ‘2022-01-01 12:00:00.0000000+05:30’ for India Standard Time). Here is an example:
DECLARE @offsetdatetime_var OFFSETDATETIME = '2022-01-01 12:00:00.0000000+05:30'
You can perform various operations on OFFSETDATETIME values, such as adding or subtracting days, hours, minutes, seconds, or nanoseconds, comparing dates and times, and converting dates and times to other data types. We will cover these operations in detail in the following sections.
Datetime Functions in SQL Server
SQL Server provides a rich set of built-in functions for working with datetime values. Here are some of the most commonly used functions:
Function | Description |
---|---|
GETDATE() | Returns the current system date and time |
SYSDATETIME() | Returns the current system date and time with high precision |
SYSDATETIMEOFFSET() | Returns the current system date and time with time zone offset |
DATEADD() | Adds or subtracts a specified interval to a date or time value |
DATEDIFF() | Returns the number of specified date or time intervals between two dates or times |
DATEPART() | Returns a specified part of a date or time value (e.g., year, month, day, hour, minute, second) |
CONVERT() | Converts a datetime value from one data type to another |
FORMAT() | Formats a datetime value according to a specified format string |
GETDATE()
The GETDATE() function returns the current system date and time in DATETIME format. Here is an example:
SELECT GETDATE()
This will return something like:
2022-01-01 12:00:00.000
You can also assign the result of GETDATE() to a DATETIME variable, as follows:
DECLARE @now DATETIME = GETDATE()
Now, @now will contain the current system date and time.
SYSDATETIME()
The SYSDATETIME() function returns the current system date and time in DATETIME2 format with high precision. Here is an example:
SELECT SYSDATETIME()
This will return something like:
2022-01-01 12:00:00.0000000
You can also assign the result of SYSDATETIME() to a DATETIME2 variable, as follows:
DECLARE @now DATETIME2 = SYSDATETIME()
Now, @now will contain the current system date and time with high precision.
SYSDATETIMEOFFSET()
The SYSDATETIMEOFFSET() function returns the current system date and time in OFFSETDATETIME format with time zone offset. Here is an example:
SELECT SYSDATETIMEOFFSET()
This will return something like:
2022-01-01 12:00:00.0000000+05:30
You can also assign the result of SYSDATETIMEOFFSET() to an OFFSETDATETIME variable, as follows:
DECLARE @now OFFSETDATETIME = SYSDATETIMEOFFSET()
Now, @now will contain the current system date and time with time zone offset.
DATEADD()
The DATEADD() function adds a specified interval to a date or time value. The syntax is:
DATEADD(interval, number, date)
where interval can be YEAR, QUARTER, MONTH, DAY, HOUR, MINUTE, SECOND, MILLISECOND, and number is the number of intervals to add or subtract (negative number for subtraction). Here is an example:
SELECT DATEADD(MONTH, 6, '2022-01-01')
This will add 6 months to the given date and return:
2022-07-01
You can also add fractions of an interval, as follows:
SELECT DATEADD(HOUR, 5.5, '2022-01-01 12:00:00')
This will add 5 hours and 30 minutes to the given date and return:
2022-01-01 05:30:00.000
DATEDIFF()
The DATEDIFF() function returns the number of specified date or time intervals between two dates or times. The syntax is:
DATEDIFF(interval, startdate, enddate)
where interval can be YEAR, QUARTER, MONTH, DAY, HOUR, MINUTE, SECOND, MILLISECOND. Here is an example:
SELECT DATEDIFF(DAY, '2022-01-01', '2022-01-31')
This will return the number of days between the two dates, which is:
30
You can also use DATEDIFF() to calculate the difference between two times, as follows:
SELECT DATEDIFF(SECOND, '2022-01-01 12:00:00', '2022-01-01 12:00:10')
This will return the number of seconds between the two times, which is:
10
DATEPART()
The DATEPART() function returns a specified part of a date or time value. The syntax is:
DATEPART(interval, date)
where interval can be YEAR, QUARTER, MONTH, DAY, HOUR, MINUTE, SECOND, MILLISECOND. Here is an example:
SELECT DATEPART(YEAR, '2022-01-01')
This will return the year part of the given date, which is:
2022
You can also use DATEPART() to extract other parts of a date or time value, as follows:
SELECT DATEPART(MINUTE, '2022-01-01 12:34:56')
This will return the minute part of the given time, which is:
34
CONVERT()
The CONVERT() function converts a datetime value from one data type to another. The syntax is:
CONVERT(datatype, expression, style)
where datatype can be any of the SQL Server datetime data types, expression is the datetime value you want to convert, and style is an optional parameter that specifies the format of the datetime value. Here is an example:
SELECT CONVERT(DATE, '2022-01-01 12:00:00.000')
This will convert the given datetime value to the DATE data type and return:
2022-01-01
You can also use CONVERT() to convert datetime values between different data types, as follows:
SELECT CONVERT(TIME, '2022-01-01 12:00:00.000')
This will convert the given datetime value to the TIME data type and return:
12:00:00.0000000
FORMAT()
The FORMAT() function formats a datetime value according to a specified format string. The syntax is:
FORMAT(expression, formatstring)
where expression is the datetime value you want to format, and formatstring is the format string that specifies the desired output format. Here is