- T-SQL - Numeric Functions
- T-SQL - Date Functions
- T-SQL - String Functions
- T-SQL - Functions
- T-SQL - Indexes
- T-SQL - Transactions
- T-SQL - Stored Procedures
- T-SQL - Sub-Queries
- T-SQL - Joining Tables
- T-SQL - DISTINCT Clause
- T-SQL - GROUP BY Clause
- T-SQL - ORDER BY Clause
- T-SQL - LIKE Clause
- T-SQL - WHERE Clause
- T-SQL - DELETE Statement
- T-SQL - UPDATE Statement
- T-SQL - SELECT Statement
- T-SQL - INSERT Statement
- T-SQL - Drop Tables
- T-SQL - Create Tables
- T-SQL - Data Types
- T-SQL - Overview
- T-SQL - Home
T-SQL Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
T-SQL - Date Functions
Following is the pst of date functions in MS SQL Server.
GETDATE()
It will return the current date along with time.
Syntax
Syntax for the above function −
GETDATE()
Example
The following query will return the current date along with time in MS SQL Server.
Select getdate() as currentdatetime
DATEPART()
It will return the part of date or time.
Syntax
Syntax for the above function −
DATEPART(datepart, datecolumnname)
Example
Example 1 − The following query will return the part of current date in MS SQL Server.
Select datepart(day, getdate()) as currentdate
Example 2 − The following query will return the part of current month in MS SQL Server.
Select datepart(month, getdate()) as currentmonth
DATEADD()
It will display the date and time by add or subtract date and time interval.
Syntax
Syntax for the above function −
DATEADD(datepart, number, datecolumnname)
Example
The following query will return the after 10 days date and time from the current date and time in MS SQL Server.
Select dateadd(day, 10, getdate()) as after10daysdatetimefromcurrentdatetime
DATEDIFF()
It will display the date and time between two dates.
Syntax
Syntax for the above function −
DATEDIFF(datepart, startdate, enddate)
Example
The following query will return the difference of hours between 2015-11-16 and 2015-11-11 dates in MS SQL Server.
Select datediff(hour, 2015-11-16, 2015-11-11) as differencehoursbetween20151116and20151111
CONVERT()
It will display the date and time in different formats.
Syntax
Syntax for the above function −
CONVERT(datatype, expression, style)
Example
The following queries will return the date and time in different format in MS SQL Server.
SELECT CONVERT(VARCHAR(19),GETDATE()) SELECT CONVERT(VARCHAR(10),GETDATE(),10) SELECT CONVERT(VARCHAR(10),GETDATE(),110)Advertisements