如何在sql中的特定日期选择给定日期的下一个日期 [英] how to select dates next of a given date on a particular day in sql

查看:206
本文介绍了如何在sql中的特定日期选择给定日期的下一个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我在选择一些日期时遇到问题。我的程序如下所示

Hello hi to all.
I have a problem in selecting some dates.I have a procedure as given below

Create procedure Select_Disurbment_Dates

as
begin

declare @max_Id int,@previousDate datetime,@current_system_date datetime, @Min_disburment_date datetime
set @max_Id=(select MAX(dayopencloseid)  from DayOpenDayCloseInfo
where CollectionByLoanOfficerStatus=1 and DayCloseByAdminStatus=1 )

set @previousDate=(select CenterCollectionDate from DayOpenDayCloseInfo where dayopencloseid=@max_Id)

set @current_system_date=(Select  DATEADD(d,1, @previousDate) as Current_SystemDate )

set @Min_disburment_date=(Select  DATEADD(d,14, @current_system_date)  )
   select @Min_disburment_date as MinDisburment_Date

end



这给我一个结果作为日期



结果是 2013-08-21 00:00:00.000



i想要代表此日期选择接下来的三个日期任何一天(可能是接下来三个即将到来的星期一星期日日期大于此日期(即 2013-08-21 00:00:00.000





如何找到我想要的结果..请告诉我frnds ...


this give me a result as a date

result is 2013-08-21 00:00:00.000

i want to select the next three dates of this date on behalf of any day (may be next three upcoming monday or sunday date which is greater than this dates(i.e. 2013-08-21 00:00:00.000)


How can i find my desired result .. pls Tell me frnds ...

推荐答案

你可以使用公用表表达式 [ ^ ]:

You can acieve that using Common Table Expressions[^]:
Create procedure Select_Disurbment_Dates
       @inputDate DATETIME   
AS
BEGIN
    --SET DATEFORMAT ymd;

    ;WITH Next3Days AS
    (
	SELECT @inputDate AS MyDate, 0 AS Counter
	UNION ALL
	SELECT DATEADD(dd,1,MyDate), Counter+1 AS Counter
	FROM Next3Days
	WHERE COunter<3
    )
    SELECT CONVERT(VARCHAR(10),MyDate,121) AS MyDate, Counter
    FROM Next3Days
    WHERE Counter>0
END





结果:



Result:

2013-08-22	1
2013-08-23	2
2013-08-24	3





或使用同时 [ ^ ]循环和临时表;)



Or use while[^] loop and temporary table ;)


这对你也很有用



http://stackoverflow.com/questions/ 12638256 / how-to-select-last-three-month-all-date-in-sql-server [ ^ ]



http://stackoverflow.com/questions/5196822/select-最近3个月的最大和最小日期 [ ^ ]



HTTP:// drupal的.stackexchange.com / questions / 35200 / how-do-i-query-for-next-three-dates-using-entityfieldquery-or-views-7 [ ^ ]

问候..:)
this may also useful to you

http://stackoverflow.com/questions/12638256/how-to-select-last-three-month-all-dates-in-sql-server[^]

http://stackoverflow.com/questions/5196822/select-the-maximal-and-minimum-dates-in-the-last-3-months[^]

http://drupal.stackexchange.com/questions/35200/how-do-i-query-for-the-next-three-dates-using-entityfieldquery-or-views-7[^]
regards ..:)


这篇关于如何在sql中的特定日期选择给定日期的下一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆