注:我的数据库时sql server的
有张order表,里面有三个属性,一个id,一个startdate,一个enddate。其中后面两个日期的格式是1/31/2010,即月/日/年
我现在想要取到每个月所在的所以数据
比如:6月份的,6月1号在startdate和enddate中的这条数据,到6月底那天所在的那条数据,中间的也全部都要。
我自己写了一个,不过有点问题,就是在取不是1月或12月的时候,每次都会多出一条该年最后一个月的最后一条数据,为什么呢?sql语句如下:
select w.id,cast(w.startdate as datetime) startdate,cast(w.enddate as datetime) enddate
from order w where cast(year(w.startdate) as varchar)+'-'+cast(month(w.startdate) as varchar) <= '2010-7'
and cast(year(w.enddate) as varchar)+'-'+cast(month(w.enddate) as varchar) >= '2010-7'
取得的效果如下,多了最后那条数据:
183 2010-06-28 00:00:00.000 2010-07-04 00:00:00.000
184 2010-07-05 00:00:00.000 2010-07-11 00:00:00.000
185 2010-07-12 00:00:00.000 2010-07-18 00:00:00.000
186 2010-07-19 00:00:00.000 2010-07-25 00:00:00.000
187 2010-07-26 00:00:00.000 2010-08-01 00:00:00.000
209 2010-12-27 00:00:00.000 2011-01-02 00:00:00.000
请高人指点下,谢谢!