我们可以使用 DATEPART 函数获取 SQL Week 开始日期的年份、周数和星期几,然后再通过一些计算得到该周的开始日期。 DECLARE @input_date DATE = '2022-01-08'; SELECT DATEADD(DAY, 1-DATEPART(WEEKDAY, @input_date), @input_date) AS sql_week_start_date; 复制 输出结果为: | sql_week_start_date | ...
要显示周的开始日期,可以先使用DATEPART函数获取给定日期的周几,然后使用DATEADD函数将该天数减去,得到周的开始日期。 以下是一个示例查询,假设有一个名为"orders"的表,其中包含一个名为"order_date"的日期字段: 代码语言:sql 复制 SELECTDATEADD(DAY,1-DATEPART(WEEKDAY,order_date),order_date)ASweek_start_date...
(casewhen TO_DATE(WEEK_START_DATE,'yyyy-mm-dd') < TO_DATE('2018-08-01','yyyy-mm-dd') then'2018-08-01'elseWEEK_START_DATE end)asWEEKS_START_DATE , (casewhen TO_DATE(WEEK_END_DATE,'yyyy-mm-dd') > TO_DATE('2018-09-15','yyyy-mm-dd') then'2018-09-15'elseWEEK_END_DATE ...
Week Start Date using Sql Query SELECT DATEADD(DAY, 2 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [Week_Start_Date] Divide above Sql Query by passing parameter value select DATEPART(WEEKDAY, GETDATE()) select CAST(GETDATE() AS DATE) SELECT ...
SET@IWeek=@IWeek+1 SET@WSDate=@WSDate+7 SET@WEDate=@WEDate+7 END SET@SYear=@SYear+1 END--END WHILE YEAR 3.生成的tWeek表内部分数据示例 顺序号 年份 第几周 该周开始日期 该周结束日期 tSeqID tYear tWeekSeq tStartDate tEndDate ...
SET@WEDate=DATEADD(DAY,6,@WSDate)--本周结束日期 WHILEDATEDIFF(DAY,@WSDate,@YEDate)>=4 BEGIN --执行Insert INSERTINTOtWeek(tYear, tWeekSeq, tStartDate, tEndDate) VALUES(@SYear,@IWeek,@WSDate,@WEDate) --重置参数 SET@IWeek=@IWeek+1 ...
date:日历日期,包括年(四位),月和日。 time: 一天中的时间,包括小时,分和秒。可以用变量time(...
SET @DATE_COUNT = DATEDIFF(DAY,@START_DATE,@END_DATE); --建立循环,条件是@i小于一年的天数 WHILE @i < @DATE_COUNT BEGIN INSERT INTO CALENDAR_INFO (COUNTRY,CDR_CODE,DATE_NAME, YEAR,MONTH,WEEK,DAY,CDR_DATE,CREATOR,CREATE_DATE,CREATE_INST) ...
day(current_date())) as startdate, date_sub(current_date(), weekday(current_date()) + 1) as enddate union SELECT '上上周' as weekname, date_sub(current_date(), 14 + weekday(current_date())) as startdate, date_sub(current_date(), weekday(current_date()) + 8) as enddate...
SELECT @LastWeekEndTime= CONVERT(nvarchar(10), DATEADD(wk, DATEDIFF(wk,0,DATEADD(dd, -7, getdate()) ), 6),121)--上周结束时间 select @ThisWeekStartTime,@ThisWeekEndTime,@LastWeekStartTime,@LastWeekEndTime SELECT @ThisMonthStartTime=CONVERT(nvarchar(10),dateadd(dd,-day(getdate())+1,get...