DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Use Of DATEPART() Function In Sql Server

12.27.2011
| 9189 views |
  • submit to reddit
        The DATEPART() function is used to return a single part of a date/time, such as year, month, day, hour, minute, etc.

Syntax
DATEPART(datepart,date)

The following SELECT statement:
SELECT DATEPART(yyyy,OrderDate) AS OrderYear,
DATEPART(mm,OrderDate) AS OrderMonth,
DATEPART(dd,OrderDate) AS OrderDay,
FROM Orders
WHERE OrderId=1 

OUTPUT
OrderYear	OrderMonth	OrderDay
2008	        11	        11

<a href="http://www.java-forums.org/blogs/spring-framework/"><strong>Spring Framework</strong></a>