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:
Examples of how to use sql datepart
To get today's date you would use the following statement:
The result would be something like:
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.