在SQL中,可以使用YEAR()和WEEK()函數(shù)來提取年份中的周次。以下是一個示例查詢,假設(shè)我們有一個名為"orders"的表,其中包含一個名為"order_date"的日期列:
SELECT
YEAR(order_date) AS order_year,
WEEK(order_date) AS order_week
FROM
orders;
這個查詢將返回一個結(jié)果集,其中包含兩列:order_year(訂單日期的年份)和order_week(訂單日期的周次)。請注意,不同的數(shù)據(jù)庫系統(tǒng)可能對WEEK()函數(shù)的行為有所不同。上述示例適用于MySQL和PostgreSQL。對于SQL Server,可以使用以下查詢:
SELECT
YEAR(order_date) AS order_year,
DATEPART(wk, order_date) AS order_week
FROM
orders;
對于Oracle,可以使用以下查詢:
SELECT
EXTRACT(YEAR FROM order_date) AS order_year,
TO_CHAR(order_date, 'WW') AS order_week
FROM
orders;