当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


SQL Server DATENAME()用法及代码示例

DATENAME()函数:

SQL Server中的此函数用于查找指定日期的给定部分。此外,它以字符串形式返回输出值。

特征:

  • 此函数用于查找指定日期的给定部分。
  • 此函数位于日期函数下。
  • 该函数接受两个参数,即间隔和日期。
  • 此函数还可以在日期部分中包含时间。
  • 此函数以字符串形式返回输出。

用法:

DATENAME(interval, date)

参数:



此方法接受以下两个参数。

  • 间隔-这是要返回的指定零件。此外,间隔的值可以如下所示。
year, yyyy, yy   = Year, which is the specified year.
quarter, qq, q   = Quarter, which is the specified quarter.
month, mm, m     = month, which is the specified month.
dayofyear, dy, y = Day of the year, which is the specified day of the year.
day, dd, d       = Day, which is the specified day.
week, ww, wk     = Week, which is the specified week.
weekday, dw, w   = Weekday, which is the specified week day.
hour, hh         = hour, which is the specified hour.
minute, mi, n    = Minute, which is the specified minute.
second, ss, s    = Second, which is the specified second.
millisecond, ms  = Millisecond, which is the specified millisecond.
  • 日期-这是要使用的指定日期。

返回值:

它返回指定日期的给定部分。

示例1:

使用DATENAME()函数并获取指定日期的年份部分。

SELECT DATENAME(year, '2021/01/06');

输出:

2021

示例-2:

使用DATENAME()函数并获取指定日期的月份部分。



SELECT DATENAME(month, '2021/01/06');

输出:

January

示例3:

使用DATENAME()函数并获取指定日期的日期部分。

SELECT DATENAME(day, '2021/01/06');

输出:

6

示例4:

使用DATENAME()函数并获取指定日期的小时部分,其中还包括时间。

SELECT DATENAME(hour, '2021/01/06 05:30');

输出:

5

示例5:

使用DATENAME()函数并使用变量获取指定日期的第二部分,其中包括时间。

DECLARE @date VARCHAR(50);
SET @date = '2019/06/05 07:37:54';
SELECT DATENAME(second, @date);

输出:

54

应用:

此函数用于查找指定日期的给定部分。

相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 DATENAME() Function in SQL Server。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。