Skip to content

Commit 89289b9

Browse files
authored
Update datename-transact-sql.md
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!
1 parent 56cdcf3 commit 89289b9

1 file changed

Lines changed: 36 additions & 17 deletions

File tree

docs/t-sql/functions/datename-transact-sql.md

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ monikerRange: ">= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest |
3434
# DATENAME (Transact-SQL)
3535
[!INCLUDE[tsql-appliesto-ss2008-all-md](../../includes/tsql-appliesto-ss2008-all-md.md)]
3636

37-
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.
4040

4141
![Topic link icon](../../database-engine/configure-windows/media/topic-link.gif "Topic link icon") [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
4242

@@ -48,7 +48,10 @@ DATENAME ( datepart , date )
4848

4949
## Arguments
5050
*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.
5255
5356
|*datepart*|Abbreviations|
5457
|---|---|
@@ -69,8 +72,17 @@ Is the part of the *date* to return. The following table lists all valid *datepa
6972
|**ISO_WEEK**|**ISOWK, ISOWW**|
7073

7174
*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.
7486

7587
## Return Type
7688
**nvarchar**
@@ -79,20 +91,20 @@ To avoid ambiguity, use four-digit years. For information about two-digit years,
7991

8092
- Each *datepart* and its abbreviations return the same value.
8193

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.
8395

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).
8597

8698
## 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.
88100

89101
## 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.
91103

92104
## 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 .
94106

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`.
96108

97109
```sql
98110
SELECT DATENAME(year, '12:10:30.123')
@@ -102,20 +114,27 @@ SELECT DATENAME(year, '12:10:30.123')
102114
,DATENAME(weekday, '12:10:30.123');
103115
```
104116

105-
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:
106118

107119
```sql
108120
DECLARE @t time = '12:10:30.123';
109121
SELECT DATENAME(year, @t);
110122
```
111123

112124
## 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
114133

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.
116135

117136
## 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:
119138

120139
`SELECT DATENAME(datepart,'2007-10-30 12:15:32.1234567 +05:10');`
121140

@@ -141,7 +160,7 @@ The following example returns the date parts for the specified date.
141160

142161
[!INCLUDE[ssSDWfull](../../includes/sssdwfull-md.md)] and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
143162

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:
145164

146165
```sql
147166
SELECT DATENAME(datepart,'2007-10-30 12:15:32.1234567 +05:10');

0 commit comments

Comments
 (0)