Datepart function in SQL Server 2008

In this article I describe about DATEPART() function in SQL.
  • 3085

Introduction

DATEPART() Function is used to retreive only a part of date or time from the datetime variable. Let us take practical example to implement DATEPART function.

Syntax

DATEPART(datepart, date)

where,

datepart- denotes which part is to be returned. It can be date, month or year.

Example

SELECT DATEPART(year, '12-07-1988') AS 'Year'

SELECT DATEPART(year, GETDATE()) AS 'Year'

SELECT DATEPART(month, GETDATE()) AS 'Month'

SELECT DATEPART(hour, GETDATE()) AS 'Hour'

Output:

datepart_function-in-sql.jpg

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.