當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。