SQL Server DATEPART() 函數 (取出日期時間中特定的部分)
DATEPART() 是 SQL Server 中用來從日期時間值中取出特定部分的函數。它可以取出年、月、日、小時、分鐘、秒、週數、季度等各種日期時間元素。
DATEPART() 語法 (Syntax)
DATEPART(datepart, date)
- datepart:要取出的日期時間部分(見下方表格)。
- date:日期或日期時間表達式。
- 返回值:整數。
datepart 可用的值
| datepart (全名) | 縮寫 | 說明 | 範例值 |
|---|---|---|---|
| year | yy, yyyy | 年份 | 2024 |
| quarter | qq, q | 季度(1-4) | 4 |
| month | mm, m | 月份(1-12) | 11 |
| dayofyear | dy, y | 一年中的第幾天(1-366) | 327 |
| day | dd, d | 日期(1-31) | 22 |
| week | wk, ww | 一年中的第幾週(1-53) | 47 |
| weekday | dw | 星期幾(1=週日, 7=週六) | 6 |
| hour | hh | 小時(0-23) | 14 |
| minute | mi, n | 分鐘(0-59) | 30 |
| second | ss, s | 秒(0-59) | 45 |
| millisecond | ms | 毫秒(0-999) | 123 |
| microsecond | mcs | 微秒(0-999999) | 123456 |
| nanosecond | ns | 奈秒(0-999999999) | 123456700 |
| TZoffset | tz | 時區偏移(分鐘) | 480 |
| ISO_WEEK | isowk, isoww | ISO 週數(1-53) | 47 |
DATEPART() 函數用法範例 (Example)
取出年份
SELECT DATEPART(year, '2024-11-22 14:30:45');
-- 結果:2024
SELECT DATEPART(yyyy, GETDATE());
-- 結果:2024
取出月份
SELECT DATEPART(month, '2024-11-22');
-- 結果:11
SELECT DATEPART(mm, '2024-11-22');
-- 結果:11
取出日期
SELECT DATEPART(day, '2024-11-22');
-- 結果:22
SELECT DATEPART(dd, '2024-11-22');
-- 結果:22
取出小時、分鐘、秒
SELECT DATEPART(hour, '2024-11-22 14:30:45');
-- 結果:14
SELECT DATEPART(minute, '2024-11-22 14:30:45');
-- 結果:30
SELECT DATEPART(second, '2024-11-22 14:30:45');
-- 結果:45
取出季度
SELECT DATEPART(quarter, '2024-01-15'); -- 結果:1
SELECT DATEPART(quarter, '2024-05-15'); -- 結果:2
SELECT DATEPART(quarter, '2024-08-15'); -- 結果:3
SELECT DATEPART(quarter, '2024-11-15'); -- 結果:4
取出星期幾
SELECT DATEPART(weekday, '2024-11-22');
-- 結果:6(週五,預設 1=週日)
-- 注意:weekday 的值取決於 @@DATEFIRST 設定
SET DATEFIRST 1; -- 設定週一為一週的第一天
SELECT DATEPART(weekday, '2024-11-22');
-- 結果:5(週五)
取出週數
SELECT DATEPART(week, '2024-11-22');
-- 結果:47
SELECT DATEPART(ISO_WEEK, '2024-11-22');
-- 結果:47(ISO 週數)
實際應用範例
從訂單日期取出各部分
假設有一個 orders 資料表:
SELECT
order_id,
order_date,
DATEPART(yyyy, order_date) AS order_year,
DATEPART(mm, order_date) AS order_month,
DATEPART(dd, order_date) AS order_day,
DATEPART(hh, order_date) AS order_hour
FROM orders;
| order_id | order_date | order_year | order_month | order_day | order_hour |
|---|---|---|---|---|---|
| 1 | 2024-11-22 14:30:00 | 2024 | 11 | 22 | 14 |
按年份統計
SELECT
DATEPART(year, order_date) AS year,
COUNT(*) AS order_count
FROM orders
GROUP BY DATEPART(year, order_date)
ORDER BY year;
按月份統計
SELECT
DATEPART(year, order_date) AS year,
DATEPART(month, order_date) AS month,
COUNT(*) AS order_count,
SUM(amount) AS total_amount
FROM orders
GROUP BY DATEPART(year, order_date), DATEPART(month, order_date)
ORDER BY year, month;
按季度統計
SELECT
DATEPART(year, order_date) AS year,
DATEPART(quarter, order_date) AS quarter,
SUM(amount) AS total_amount
FROM orders
GROUP BY DATEPART(year, order_date), DATEPART(quarter, order_date)
ORDER BY year, quarter;
按星期幾統計
SELECT
DATEPART(weekday, order_date) AS weekday_num,
DATENAME(weekday, order_date) AS weekday_name,
COUNT(*) AS order_count
FROM orders
GROUP BY DATEPART(weekday, order_date), DATENAME(weekday, order_date)
ORDER BY weekday_num;
按小時分析訂單分佈
SELECT
DATEPART(hour, order_date) AS hour,
COUNT(*) AS order_count
FROM orders
GROUP BY DATEPART(hour, order_date)
ORDER BY hour;
DATEPART() 與 DATENAME() 的比較
DATEPART() 返回整數,DATENAME() 返回字串(文字名稱):
SELECT
DATEPART(month, '2024-11-22') AS month_number, -- 結果:11
DATENAME(month, '2024-11-22') AS month_name; -- 結果:November
SELECT
DATEPART(weekday, '2024-11-22') AS weekday_number, -- 結果:6
DATENAME(weekday, '2024-11-22') AS weekday_name; -- 結果:Friday
簡化函數
SQL Server 也提供了一些專用函數,功能與 DATEPART() 相同:
-- 以下查詢結果相同
SELECT DATEPART(year, GETDATE());
SELECT YEAR(GETDATE());
SELECT DATEPART(month, GETDATE());
SELECT MONTH(GETDATE());
SELECT DATEPART(day, GETDATE());
SELECT DAY(GETDATE());