SQL Date Data type
SQL Server uses the following data types to store date or date/time values in the database:
- DATE-Format YYYY-MM-DD
- DATETIME-format: Yyyy-mm-dd HH:MM:SS
- smalldatetime-format: Yyyy-mm-dd HH:MM:SS
- TIMESTAMP-Format: Unique number
Use the following SELECT statement:
SELECT GETDATE () as Currentdatetime
The DATEPART () function is used to return a separate part of a date/time, such as year, month, day, hour, minute, and so on.
Grammar
DATEPART (DATEPART,date)
The DATEADD () function adds or subtracts a specified time interval from a date.
Grammar
DATEADD (datepart,number,date)
The date parameter is a valid day expression. The number is the number of intervals you want to add, and for the future time, this is positive, and for the past time, this number is negative.
The datepart parameter can be the following value:
The DATEDIFF () function returns the number of days between two dates.
Grammar
DATEDIFF (datepart,startdate,enddate)
The startdate and enddate parameters are valid date expressions.
The convert () function is a general function that converts a date to a new data type.
The CONVERT () function can display date/time data in different formats.
Grammar
CONVERT (data_type (length),data_to_be_converted,style)
data_type (length) Specifies the target data type (with optional lengths). The data_to_be_converted contains values that need to be converted. style Specifies the date/time output format.
SQL Date function