如何根据用户输入日期获取周开始日期 [英] How to get Week start date based on user input date
问题描述
亲爱的先生,
如果我输入任何日期,如何在SQL查询中获取周开始日期。
假设我选择5-Dec-13,我想根据输入日期显示周开始日期(30-Nov-13)。意思是,我希望星期六作为星期开始日期(30-Nov-13)到星期五结束日期(5月12日 - 13日)。
只有我想要显示一周根据SQL查询中的用户输入日期开始日期。
您提供的示例目前还不清楚,但请看一下:
声明 @ d datetime
set @ d = ' 2013-12-05'
选择
DATEADD(dd , - (DATEPART(dw, @ d ) - 1), @ d )[WeekStart],
DATEADD(dd,7-(DATEPART(dw, @ d )), @ d ) [WeekEnd]
它将在2013-12-01(星期日)作为周开始,并在2013-12-07(星期六)作为w返回eek结束。如果你想要自定义周开始,请使用常量。
[更新]
这是修改后的版本,可以开始一周周六和周五结束:
选择
DATEADD(dd, - (DATEPART( dw, @ d )), @ d )[WeekStart],
DATEADD(dd, 6-(DATEPART(dw, @ d )), @ d )[WeekEnd]
要获得一周开始的日期,这里有三个例子:
将GetDate()
替换为您的日期列名称或日期值。
选择 DateAdd(wk,DateDiff(wk, 0 ,GetDate()) - 1 , 0 ) LastWeekStart日期
选择 DateAdd(wk,DateDiff(wk, 0 ,GetDate()),< span class =code-digit> 0 ) as ThisWeekStartDate
选择 DateAdd(wk,DateDiff(wk, 0 ,GetDate())+ 1 , 0 ) NextWeekStartDate
注意:结果将取决于SQL Server的@@ DATEFIRST
参数。
DATEADD函数只允许您添加或减去指定日期/时间值的指定时间单位。
DATEADD功能的格式如下:
DATEADD(<时间单位>,< Units>,< Date>)
- 将5天添加到9月1日,2011年的功能将是
DATEADD(DAY,5,'9 / 1/2011')
- 从2011年9月1日开始扣除5个月的功能将是
DATEADD(月, - 5,'9/1/2011')
选择DATEADD(DAY,-5,'12 / 5/2013')作为WeakStartDate
Dear sir,
How to get week start date in SQL queries, if i am entering any date.
suppose i select 5-Dec-13, i want to display week start date(30-Nov-13) based on input date. means,i want saturday as week start date(30-Nov-13) to end date friday(5-Dec-13).
only i want to dispaly week start date based on user input date in SQL Queries.
The sample you had given is quite unclear, but have a look here:
declare @d datetime set @d='2013-12-05' select DATEADD(dd, -(DATEPART(dw, @d)-1), @d) [WeekStart], DATEADD(dd, 7-(DATEPART(dw, @d)), @d) [WeekEnd]
It will return 2013-12-01 (Sunday) as week start, and 2013-12-07 (Saturday) as week end. If you want custom week start, play with the constants.
[Update]
Here is the modified version that makes a week to start on Saturday and end on Friday:
select DATEADD(dd, -(DATEPART(dw, @d)), @d) [WeekStart], DATEADD(dd, 6-(DATEPART(dw, @d)), @d) [WeekEnd]
To get the date of the day that is the start of the week, here are three examples:
ReplaceGetDate()
with your date column name or date value.
Select DateAdd(wk, DateDiff(wk, 0, GetDate()) - 1, 0) as LastWeekStartDate Select DateAdd(wk, DateDiff(wk, 0, GetDate()), 0) as ThisWeekStartDate Select DateAdd(wk, DateDiff(wk, 0, GetDate()) + 1, 0) as NextWeekStartDate
Note: The result will depend on the SQL Server's@@DATEFIRST
parameter.
The DATEADD function simply allows you to add or subtract the specified number of units of time to a specified date/time value.
The format of the DATEADD function is as follows:
DATEADD(<Unit of time>, <Units>, <Date>)
-- to add 5 days to September 1, 2011 the function would be
DATEADD(DAY, 5, '9/1/2011')
-- to subtract 5 months from September 1, 2011 the function would be
DATEADD(MONTH, -5, '9/1/2011')
select DATEADD(DAY,-5, '12/5/2013') as WeakStartDate
这篇关于如何根据用户输入日期获取周开始日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!