SQL Server中日期变量通常包含着日期和时间两块信息,但是在很多处理中我们并不需要时间信息,此时可以利用转换函数舍弃时间信息并只保留日期信息,如
convert(char(10),date1,111)
将日期变量date1的转换成诸如2012/11/03这样的形式
借此方法,可以实现更多功能,如下面的语句可以查询newlendfull表中每条记录中date1和date2日期之间发生的其他记录个数,汇总统计并更新到现有的每记录midcount字段中
update newlendfull set midcount=(select count(distinct convert(char(10),date1,111)) from newlendfull a
where convert(char(10),date1,111)>convert(char(10),newlendfull.date1,111) and convert(char(10),date1,111)<convert(char(10),newlendfull.date2,111) and a.rid=newlendfull.rid)