SQL

How To Use Date Functions In SQL

In this article, we will learn how to use the Date Functions in SQL.

SQL Server has many built-in Date Functions.

Function Description
CURRENT_TIMESTAMP() It returns the current date and time.
SELECT CURRENT_TIMESTAMP AS CurrentDateTime;

Output: 2019-12-10 12:14:29.110

DATEADD() It adds a date/time interval to date and then returns the date.
SELECT DATEADD(MONTH, 1, '2019/12/10') AS AddDate;

Output: 2020-01-10 00:00:00.000

DATEDIFF() It returns the difference between two dates.
SELECT DATEDIFF(DAY, '2019/12/10', '2020/01/01') AS DiffDate;

Output: 22

DATEFROMPARTS() It returns a date from the specified parts (year, month, and day).
SELECT DATEFROMPARTS(2019, 12, 10) AS DateFromParts;

Output: 2019-12-10

DATENAME() It returns a specified part of a date in the string.
SELECT DATENAME(MONTH, '2019/12/10') AS DatePartString;

Output: December

DATEPART() It returns a specified part of a date in integer.
SELECT DATEPART(MONTH, '2019/12/10') AS DatePartInt;

Output: 12

DAY() It returns the day of the month for a specified date.
SELECT DAY('2019/12/10') AS DayOfMonth;

Output: 10

GETDATE() It returns the current database system date and time.
SELECT GETDATE() AS CurrentDateTime;

Output: 2019-12-10 12:14:29.110

GETUTCDATE() It returns the current database system UTC date and time.
SELECT GETUTCDATE() AS CurrentUTCDateTime;

Output: 2019-12-10 06:55:54.407

ISDATE() It checks an expression and returns 1 if it is a valid date, otherwise 0.
SELECT ISDATE('2019-12-32');

Output: 0

MONTH() It returns the month part for a specified date.
SELECT MONTH('2019/12/10') AS Month;

Output: 12

SYSDATETIME() It returns the date and time of the SQL Server.
SELECT SYSDATETIME() AS CurrentSysDateTime;

Output: 2019-12-10 12:28:37.3831871

YEAR() It returns the year part for a specified date.
SELECT YEAR('2019/12/10') AS Year;

Output: 2019

 

Also, check How To Use NULL Functions In SQL

Yasin Panwala

Yasin Panwala is a Web Developer and Author at TheCodeHubs. He has experience in Web Developing and Designing and also in Writing. He has got his skills in working on technologies like .NET Core, ADO.NET, AJAX, Angular, AngularJS, ASP.NET, ASP.NET MVC, Bootstrap, C#, CSS, Entity Framework, Express.js, GraphQL, HTML, JavaScript, JQuery, JSON, LINQ, Microsoft Office, MongoDB, MySQL, Node.js, PostgreSQL, SQL, SQL Server, TypeORM, TypeScript, Visual Basic .NET, Web API. He also got his skills in working with different integration and some known versioning tools. He is always ready to learn new things and he always tries his best on tasks that are assigned to him and gives the best possible outputs.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago