SQL Server 2008仅在月份和年份之间选择数据 [英] SQL Server 2008 select data only between month and year

查看:212
本文介绍了SQL Server 2008仅在月份和年份之间选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择两个日期之间的数据,没有日期



输入示例:



开始月份:9,开始年份:2011

结束月份:3,结束年份:2012



我认为有两种方法可以实现。 / p>

第一个是将开始月份和开始日期转换为 2011-09-01 并将最后一个日期转换为 2012-03-31 ,但这需要计算月末的最后一天。获得这些日期后,我们可以对WHERE子句使用BEETWEN函数(但是,CONVERT函数可靠吗?)



第二个解决方案是使用 DATEPART 函数,如以下代码所示:



我尝试解释一下:如果结束年份等于初始年份,则月份必须为在开始和结束月份之间;否则,如果最后几个月大于最初的年份(如果不同于最初和最后的年份),我将介于两者之间。否则,如果是最后一年,则该月必须小于或等于最后一个月;如果是初始年,月,则必须大于或等于最后一个月。



您能以最好的方式帮助我吗?我采用的解决方案是正确的吗?

 声明@IndDebitoCredito位,@ ProgTributo int,@ mi为整数,@ ai为整数,@ mf为整数,@ af为整数,@ IDAnagrafica varchar(5)
select @mi = 01,@ ai = 2011,@ mf = 12,@ af = 2011,@ IDAnagrafica ='DELEL',@ IndDebitoCredito = 1
从TBWH_Delega d
中选择不同的rrd.IDTributo
-在d.IDDelega = sd.IDDelega
内部加入TBWH_SeeleoneDelega sd在rd。 d.IDDelega
内部联接TBWH_RataRigo rrd在rrd.IDRigo = rd.IDRigo
其中

DATEPART(MM,d.DataDelega)< = @ mf和
DATEPART(MM,d.DataDelega)> = @ mi和
DATEPART(YYYY,d.DataDelega)= @ ai和
@af = @ai

OR

--anno finale magg。anno iniziale
@af> @ai AND

(-delega nell'intervallo
DATEPART( YYYY,d.DataDelega)< @af和
DATEPART(YYYY,d.DataDelega)> @ai
-DATEPART(MM,d.DataDelega)> = @ mi


(-delega limite destro
DATEPART(YYYY,d.DataDelega)= @ af和
DATEPART(MM,d.DataDelega)< = @ mf


(- -delega limite sinistro
DATEPART(YYYY,d.DataDelega)= @ ai和
DATEPART(MM,d.DataDelega)> == @ mi



GO

解决方案

您的第一个解决方案已经存在,但比所需解决方案复杂得多,而且无论如何都无法使用。



您可以在结束月份,然后在每个月的第一天使用BETWEEN。例如。



开始月份:9,开始年份:2011
结束月份:3,结束年份:2012



曾经'2011-09-01'和'2012-04-01'



,或者像JNK指出的那样,效果会更好:



DataDelega> ='2011 -09-01'AND DataDelega< '2012-04-01'



您需要添加一些逻辑来处理结束月份是十二月,但这似乎是最简单的方法。


I would like select data between two date, without day

An input example:

start month: 9 , start year: 2011
end month: 3, end year: 2012

I think that there are two way to do this.

The first is convert start month and start year to date like 2011-09-01 and convert last date to 2012-03-31, but this requires calculation of the last day of end month. Obtained these date we can use a BEETWEN function for the WHERE clause (but, is the CONVERT function reliable?)

The second solution is to use the DATEPART function like in the following code:

I try to explain: if end year is equal to the initial year, then month must be between the start and end months; else if the final months is greater than the initial years if different from the initial and final year, I take everything in between; else if the final year, the month must be less than or equal to the final month, if the initial year, month must be greater than or equal to the final month

Can you help me do this in the best way? Is correct, the solution I adopted?

declare @IndDebitoCredito bit,@ProgTributo int,@mi as integer,@ai as integer,@mf as integer,@af as integer,@IDAnagrafica varchar(5)
select @mi = 01,@ai = 2011,@mf = 12,@af = 2011,@IDAnagrafica = 'DELEL',@IndDebitoCredito = 1
select distinct rrd.IDTributo
    from TBWH_Delega d
    --inner join TBWH_SezioneDelega sd on d.IDDelega = sd.IDDelega
    inner join TBWH_Rigo rd on rd.IDDelega = d.IDDelega
    inner join TBWH_RataRigo rrd on rrd.IDRigo = rd.IDRigo 
    where
    (
        DATEPART(MM,d.DataDelega)<=@mf and 
        DATEPART(MM,d.DataDelega)>=@mi and 
        DATEPART(YYYY,d.DataDelega)=@ai and 
        @af = @ai
    ) 
    OR
    (
        --anno finale magg. anno iniziale
        @af > @ai AND
        ( 
            (   -- delega nell'intervallo
                DATEPART(YYYY,d.DataDelega)<@af AND 
                DATEPART(YYYY,d.DataDelega)>@ai
                -- DATEPART(MM,d.DataDelega)>=@mi 
            )
            OR
            (   -- delega limite destro
                DATEPART(YYYY,d.DataDelega)=@af AND 
                DATEPART(MM,d.DataDelega)<=@mf 
            )
            OR
            (   -- delega limite sinistro
                DATEPART(YYYY,d.DataDelega)=@ai AND 
                DATEPART(MM,d.DataDelega)>=@mi 
            )
        )
    )

GO

解决方案

Your first solution is almost there, but is more complicated than it needs to be and won't work anyway. It will miss out any rows from the last day of the end month.

You can add one month to the end month and then use BETWEEN on the first of each month. eg.

start month: 9 , start year: 2011 end month: 3, end year: 2012

BETWEEN '2011-09-01' AND '2012-04-01'

or, as JNK points out, this will be better:

DataDelega >= '2011-09-01' AND DataDelega < '2012-04-01'

You'll need to add in some logic to deal with the end month being December, but this looks like the simplest way of doing it.

这篇关于SQL Server 2008仅在月份和年份之间选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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