求两段时间之间的月数? select datediff(month,startdate,enddate) + 1 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select datediff(mm,date1,date2)+(case when (day(date2)-day(date1))<15 then 0 else 1 end) select case when day(enddate) - day(startdate) > 14 then datediff(month,startdate,enddate) + 1 else datediff(month,startdate,enddate) end create table #( dt1 varchar(10), dt2 varchar(10))insert #select '2005-01-01','2005-01-31' union select '2005-01-01','2005-02-14' unionselect '2005-01-01','2005-02-15' select dt1,dt2,[月份]=(case when (day(dt2))>=15 then 1+datediff(month,dt1,dt2) else datediff(month,dt1,dt2) end) from #drop table # sorry我没说清楚,这两段时间不一定是连续月之间,比如说2002-12-31到2005-01-01只算24个月 是这个嘛?create table #( dt1 varchar(10), dt2 varchar(10))insert #select '2005-01-01','2005-01-31' union select '2005-01-01','2005-02-14' unionselect '2005-01-01','2005-02-15' unionselect '2002-12-31','2005-01-01' select dt1,dt2,[月份]=( case when (day(dt2)-day(dt1))>=14 then datediff(month,dt1,dt2)+1 when (day(dt2)-day(dt1))<0 then datediff(month,dt1,dt2)-1 else datediff(month,dt1,dt2) end ) from #drop table #--结果/*dt1 dt2 月份 ---------- ---------- ----------- 2002-12-31 2005-01-01 242005-01-01 2005-01-31 12005-01-01 2005-02-14 12005-01-01 2005-02-15 2(所影响的行数为 4 行)*/ select date1,date2,datediff(dd,date1,date2),floor(datediff(dd,date1,date2)/30)from table1 总额区间查询 分页查询问题 如何用另一数据库里的表更新当前数据库里的表? 数据库导到空间时候存储过程出问题 表的设计问题 用ASP如何得到SQL Server存储过程的返回值并且同时得到返回的记录集? 请问两个PDM文件是否能比较里面同名table? 为什么不按聚集索引排序而按非聚集索引排序 求一SQL sql server 的日期算法提问! 邹建请进 请教一个麻烦的查询语句
then datediff(month,startdate,enddate) + 1
else datediff(month,startdate,enddate)
end
(
dt1 varchar(10),
dt2 varchar(10)
)
insert #
select '2005-01-01','2005-01-31' union
select '2005-01-01','2005-02-14' union
select '2005-01-01','2005-02-15' select dt1,dt2,[月份]=(case when (day(dt2))>=15 then 1+datediff(month,dt1,dt2) else datediff(month,dt1,dt2) end) from #drop table #
create table #
(
dt1 varchar(10),
dt2 varchar(10)
)
insert #
select '2005-01-01','2005-01-31' union
select '2005-01-01','2005-02-14' union
select '2005-01-01','2005-02-15' union
select '2002-12-31','2005-01-01' select dt1,dt2,[月份]=(
case
when (day(dt2)-day(dt1))>=14 then datediff(month,dt1,dt2)+1
when (day(dt2)-day(dt1))<0 then datediff(month,dt1,dt2)-1
else datediff(month,dt1,dt2)
end
)
from #drop table #--结果
/*dt1 dt2 月份
---------- ---------- -----------
2002-12-31 2005-01-01 24
2005-01-01 2005-01-31 1
2005-01-01 2005-02-14 1
2005-01-01 2005-02-15 2(所影响的行数为 4 行)
*/
from table1