SQL服务器日期部分 [英] SQL server date parts

查看:46
本文介绍了SQL服务器日期部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临一个严重的问题,需要一个解决方案。



我有两个日期说2015年4月15日到2015年6月10日,我希望得到所有的每月可以分手。



就像进入上述两个日期一样,我应该得到回报,例如

4月15日 - 4月30日
5月1日 - 5月31日

6月1日 - 6月10日





请咨询。

I am facing a serious issue and needs a solution.

I am having 2 dates say 15 April 2015 to 10 June 2015 and i want to get all the monthly breakups possible in between.

Like on entering two above dates i should get in return like
15 April - 30 April
1 May - 31 May
1 June - 10 June


Please Advice.

推荐答案

我认为这是你期望的输出:

以下查询可以更简单,你可以用两个日期来做变量。

i think this is what you are expecting as output:
Following query can be more simplyfy, you can do it with two date variables as well.
declare @StrtDt     smalldatetime
declare @EndDt      smalldatetime
DECLARE @WkStartDt  smalldatetime
DECLARE @WkEndDt    smalldatetime

DECLARE @Output TABLE (StartDate datetime, Enddate datetime )

set @EndDt =  '2015-06-10'
SET @WkStartDt = '2015-04-15'
WHILE @WkStartDt <= @EndDt
BEGIN
    SET @WkEndDt = dateadd(day, 0 - day(dateadd(month, 1 , @WkStartDt)), dateadd(month, 1 , @WkStartDt))

    INSERT INTO @Output(startdate,enddate)
        VALUES ( @WkStartDt, @WkEndDt)

    SET @WkStartDt = DATEADD(day, 1, @WkEndDt)
End
select   * from @Output



i希望这会对你有所帮助

谢谢


i hope this will help you
Thanks


使用startDate.Month和endDate.Month之间的循环,您应该能够生成解决方案。
With a loop between startDate.Month and endDate.Month you should be able to produce the solution.


这篇关于SQL服务器日期部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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