declare @t table(brq varchar(10),erq varchar(10),hh int,ye dec(6,2)) 
insert into @t select '2006-01-02' ,'2012-10-1',1111 ,2.01 
union all select '2006-01-05' ,'2012-10-31',1111 ,3.51 
union all select '2006-01-10' ,'2012-10-1',1111 ,2.55 
union all select '2006-01-02' ,'2012-10-30',2222 ,3.00 
union all select '2006-01-04' ,'2012-10-1',2222 ,2.00 
union all select '2006-01-05' ,'2012-10-1',3333 ,6.54 
union all select '2006-01-06' ,'2012-10-1',3333 ,5.23 
union all select '2006-01-07' ,'2012-10-1',3333 ,8.55  select * from @t where convert(nvarchar(10),getdate(),120) between brq and erq
结果我明白,取出包含当前日期区间的记录,
请问 convert(nvarchar(10),getdate(),120) between brq and erq 里的convert(nvarchar(10),getdate(),120)起什么作用
这个between是怎么运行的?

解决方案 »

  1.   

    convert(nvarchar(10),getdate(),120) 作用:
    2012-10-30 17:12:53.027 转换成 2012-10-30 
      

  2.   

    就是满足 between 前面这个值 在 brq 和erq 这个范围内的
      

  3.   

    先处理 convert(nvarchar(10),getdate(),120)  然后再筛选
      

  4.   

    select getdate(),convert(nvarchar(10),getdate(),120)
    你运行一下就知道了就是把日期的时间部分去掉
      

  5.   

    在@t里面筛选出between brq and erq  brq和erq 之间包含当前时间的行即 GETDATE()>=brq AND GETDATE()<=erq 的所有行
      

  6.   

    由于getdate()产生日期时分秒毫秒这样的数据,所以用convert来获取日期部分然后between and 会比较好,也可以使用convert(date,getdate())来直接获取,