读书人

只利用SQL日期变量的非时间信息进展处

发布时间: 2012-11-11 10:07:57 作者: rapoo

只利用SQL日期变量的非时间信息进行处理的方法例子

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)

读书人网 >SQL Server

热点推荐