You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Text revisions to tighten and optimize the reading flow of the material.
Note: the Return Value block now says
"The return value depends on SET DATEFORMAT if date is a string literal of some formats"
and 'some formats' needs explanation and definition. I looked at earlier versions of this file and they have the same problem. I Googled but I did not find a clear answer. If someone could give me some guidance, I'll revise and send a new PR.
Thank you!
Returns a character string that represents the specified *datepart* of the specified *date*
38
-
39
-
For an overview of all [!INCLUDE[tsql](../../includes/tsql-md.md)] date and time data types and functions, see [Date and Time Data Types and Functions (Transact-SQL)](../../t-sql/functions/date-and-time-data-types-and-functions-transact-sql.md).
37
+
This function returns a character string representing the specified *datepart* of the specified *date*.
38
+
39
+
See [Date and Time Data Types and Functions (Transact-SQL)](../../t-sql/functions/date-and-time-data-types-and-functions-transact-sql.md) for an overview of all [!INCLUDE[tsql](../../includes/tsql-md.md)] date and time data types and functions.
40
40
41
41
[Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
42
42
@@ -48,7 +48,10 @@ DATENAME ( datepart , date )
48
48
49
49
## Arguments
50
50
*datepart*
51
-
Is the part of the *date* to return. The following table lists all valid *datepart* arguments. User-defined variable equivalents are not valid.
51
+
The specific part of the *date* argument that `DATENAME` will return. This table lists all valid *datepart* arguments.
52
+
53
+
> [!NOTE]
54
+
> `DATENAME` does not accept user-defined variable equivalents for the *datepart* arguments.
52
55
53
56
|*datepart*|Abbreviations|
54
57
|---|---|
@@ -69,8 +72,17 @@ Is the part of the *date* to return. The following table lists all valid *datepa
69
72
|**ISO_WEEK**|**ISOWK, ISOWW**|
70
73
71
74
*date*
72
-
Is an expression that can be resolved to a **time**, **date**, **smalldatetime**, **datetime**, **datetime2**, or **datetimeoffset** value. *date* can be an expression, column expression, user-defined variable, or string literal.
73
-
To avoid ambiguity, use four-digit years. For information about two-digit years, see [Configure the two digit year cutoff Server Configuration Option](../../database-engine/configure-windows/configure-the-two-digit-year-cutoff-server-configuration-option.md).
75
+
76
+
An expression that can resolve to one of the following data types:
77
+
78
+
+**date**
79
+
+**datetime**
80
+
+**datetimeoffset**
81
+
+**datetime2**
82
+
+**smalldatetime**
83
+
+**time**
84
+
85
+
For *date*, `DATENAME` will accept a column expression, expression, string literal, or user-defined variable. Use four-digit years to avoid ambiguity issues. See [Configure the two digit year cutoff Server Configuration Option](../../database-engine/configure-windows/configure-the-two-digit-year-cutoff-server-configuration-option.md) for information about two-digit years.
74
86
75
87
## Return Type
76
88
**nvarchar**
@@ -79,20 +91,20 @@ To avoid ambiguity, use four-digit years. For information about two-digit years,
79
91
80
92
- Each *datepart* and its abbreviations return the same value.
81
93
82
-
The return value depends on the language environment set by using [SET LANGUAGE](../../t-sql/statements/set-language-transact-sql.md) and by the [Configure the default language Server Configuration Option](../../database-engine/configure-windows/configure-the-default-language-server-configuration-option.md) of the login. The return value is dependant on [SET DATEFORMAT](../../t-sql/statements/set-dateformat-transact-sql.md) if *date* is a string literal of some formats. SET DATEFORMAT does not affect the return value when the date is a column expression of a date or time data type.
94
+
The return value depends on the language environment set by using [SET LANGUAGE](../../t-sql/statements/set-language-transact-sql.md), and by the [Configure the default language Server Configuration Option](../../database-engine/configure-windows/configure-the-default-language-server-configuration-option.md) of the login. The return value depends on [SET DATEFORMAT](../../t-sql/statements/set-dateformat-transact-sql.md) if *date* is a string literal of some formats. SET DATEFORMAT does not change the return value when the date is a column expression of a date or time data type.
83
95
84
-
When the *date* parameter has a **date** data type argument, the return value depends on the setting specified by using [SET DATEFIRST](../../t-sql/statements/set-datefirst-transact-sql.md).
96
+
When the *date* parameter has a **date** data type argument, the return value depends on the setting specified by [SET DATEFIRST](../../t-sql/statements/set-datefirst-transact-sql.md).
85
97
86
98
## TZoffset datepart Argument
87
-
If *datepart* argument is **TZoffset** (**tz**) and the *date* argument has no time zone offset, 0 is returned.
99
+
If the *datepart* argument is **TZoffset** (**tz**) and the *date* argument has no time zone offset, `DATEADD` returns 0.
88
100
89
101
## smalldatetime date Argument
90
-
When *date* is [smalldatetime](../../t-sql/data-types/smalldatetime-transact-sql.md), seconds are returned as 00.
102
+
When *date* is [smalldatetime](../../t-sql/data-types/smalldatetime-transact-sql.md), `DATENAME` returns seconds as 00.
91
103
92
104
## Default Returned for a datepart That Is Not in the date Argument
93
-
If the data type of the *date* argument does not have the specified *datepart*, the default for that *datepart*will be returned only when a literal is specified for *date*.
105
+
If the data type of the *date* argument does not have the specified *datepart*, `DATENAME` will return the default for that *datepart*only if the *date* argument has a literal .
94
106
95
-
For example, the default year-month-day for any **date** data type is 1900-01-01. The following statement has date part arguments for *datepart*, a time argument for *date*, and returns `1900, January, 1, 1, Monday`.
107
+
For example, the default year-month-day for any **date** data type is 1900-01-01. This statement has date part arguments for *datepart*, a time argument for *date*, and`DATENAME` returns `1900, January, 1, 1, Monday`.
If *date* is specified as a variable or table column and the data type for that variable or column does not have the specified *datepart*, error 9810 is returned. The following code example fails because the date part year is not a valid for the **time** data type that is declared for the variable *@t*.
117
+
If *date* is specified as a variable or table column, and the data type for that variable or column does not have the specified *datepart*, `DATENAME` will return error 9810. In this example, variable *@t* has a **time** data type. The example fails because the date part year is invalid for the **time** data type:
106
118
107
119
```sql
108
120
DECLARE @t time='12:10:30.123';
109
121
SELECT DATENAME(year, @t);
110
122
```
111
123
112
124
## Remarks
113
-
DATENAME can be used in the select list, WHERE, HAVING, GROUP BY, and ORDER BY clauses.
125
+
126
+
Use `DATENAME` in the following clauses:
127
+
128
+
+ GROUP BY
129
+
+ HAVING
130
+
+ ORDER BY
131
+
+ SELECT \<list>
132
+
+ WHERE
114
133
115
-
In [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)], DATENAME implicitly casts string literals as a **datetime2** type. This means that DATENAME does not support the format YDM when the date is passed as a string. You must explicitly cast the string to a **datetime** or **smalldatetime** type to use the YDM format.
134
+
In [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)], DATENAME implicitly casts string literals as a **datetime2** type. In other words, `DATENAME` does not support the format YDM when the date is passed as a string. You must explicitly cast the string to a **datetime** or **smalldatetime** type to use the YDM format.
116
135
117
136
## Examples
118
-
The following example returns the date parts for the specified date.
137
+
This example returns the date parts for the specified date. Substitute a *datepart* value from the table for the `datepart` argument in the SELECT statement:
@@ -141,7 +160,7 @@ The following example returns the date parts for the specified date.
141
160
142
161
[!INCLUDE[ssSDWfull](../../includes/sssdwfull-md.md)] and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
143
162
144
-
The following example returns the date parts for the specified date.
163
+
Thisexample returns the date parts for the specified date. Substitute a *datepart* value from the table for the `datepart` argument in the SELECT statement:
0 commit comments