whadiz.com: What is?
Search Articles:     Home | About | Tools | Contact Us
You are here: Home > Databases > Sql Server > datepart
Board Games
Online Games
Sql Server
Visual C# in ASP.Net
Web Design

Clever Facts Section

What is sql datepart? | Sql Server datepart | Sql server datepart example | Sql datepart

What is datepart?

Sql datepart is a date and time function to get a specific part of a datetime value. Sql datepart can be used to get either the day, month, year or any other part of the datetime value. Sql datepart returns an integer value. 

What is the syntax for sql datepart?

The syntax for sql datepart is as follow:

DATEPART ( @datepart (varchar) , @date (datetime) )

@datepart is a varchar value and indicates which part of the datetime value is needed.

@datepart can be any of the following values:

yearyy, yyyy
quarterqq, q
monthmm, m
dayofyeardy, y
daydd, d
weekwk, ww
minutemi, n
secondss, s

Examples of how to use sql datepart

To get today's date you would use the following statement:

SELECT getdate();

The result would be something like:

2008-04-29 12:54:24.357

If you need to get the current year, you would use sql datepart in the following manner:

SELECT datepart(yyyy, getdate());


SELECT datepart(yy, getdate());

Both will return the result 2008.

Are there any shortcuts or synonyms that can be used for the @datepart?

The DAY, MONTH, and YEAR functions are exactly the same as DATEPART(dd, date), DATEPART(mm, date), and DATEPART(yy, date), respectively.

For the above example, yy can be substituted with the word YEAR and would result in:

SELECT datepart(YEAR, getdate());

This will also return the value 2008.

Where have I tested Sql datepart?

Sql datepart was successfully tested in both Sql Server 2000 and Sql Server 2005.

Link to this article:

Browse some more: Next Article | Next Fact | More Sql Server Articles | More Clever Facts

Copyright © 2024 whadiz.com. All rights reserved.
whadiz.com: What is sql datepart? | Sql Server datepart | Sql server datepart example | Sql datepart
Home of the answer "what is"