Returns a character string representing the specified datepart of the specified date.
DATENAME(datepart, date)
Datepart | Abbreviations |
---|---|
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 |
The weekday (dw) datepart returns the day of the week (Sunday, Monday, and so on).
Is an expression that returns a datetime or smalldatetime value, or a character string in a date format. Use the datetime data type for dates after January 1, 1753. Store as character data for earlier dates. When entering datetime values, always enclose them in quotation marks. Because smalldatetime is accurate only to the minute, when a smalldatetime value is used, seconds and milliseconds are always 0. For information about specifying dates, see datetime and smalldatetime. For information about specifying time values, see Time Formats.
If you specify only the last two digits of the year, values that are less than or equal to the last two digits of the value of the two digit year cutoff configuration option are in the same century as the cutoff year. Values that are greater than the last two digits of the value of this option are in the century that precedes the cutoff year. For example, if two digit year cutoff is 2049 (default), 49 is interpreted as 2049 and 2050 is interpreted as 1950. To avoid ambiguity, use four-digit years.
nvarchar
SQL Server automatically converts between character and datetime values as necessary, for example, when you compare a character value with a datetime value.
This example extracts the month name from the date returned by GETDATE.
SELECT DATENAME(month, getdate()) AS 'Month Name'
Here is the result set:
Month Name
------------------------------
February
CAST and CONVERT | Date and Time Functions |
Data Types |