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:
year | yy, yyyy |
quarter | qq, q |
month | mm, m |
dayofyear | dy, y |
day | dd, d |
week | wk, ww |
weekday | dw |
hour | hh |
minute | mi, n |
second | ss, s |
millisecond | ms |
|
Examples of how to use sql datepartTo 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());
or
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. |