显示数据库中的数据,数据库中有时间记录getdate(),想让用户按时间进行查询。比如给用户给选择时间的界面,可以直接选择比如2006年10月的记录,也可以选择2006年的记录,也可以选择2006年11月7日的记录,我不知道用什么办法能直接查出来,能解决一下么?急

解决方案 »

  1.   

    declare @dt as varchar(10)
    set @dt = '2006' / '2006-10' / '2006-11-07'
    convert(varchar(10) , yourdatetime , 120) like @dt
      

  2.   

    select * from tablename where convert(char(8), col, 112) like '2006%'
    select * from tablename where convert(char(8), col, 112) like '200610%'
    select * from tablename where convert(char(8), col, 112) like '20061107%'select convert(char(8), getdate(), 112)
      

  3.   

    declare @dt as varchar(10)
    set @dt = '2006' + '%' ('2006-10' + '%' or '2006-11-07' + '%')
    convert(varchar(10) , yourdatetime , 120) like @dt
      

  4.   

    if @tempday!=0 AND @tempmonth!=0 AND @tempyear!=0
        select * from ViewLog where year(ViewTime)=@tempyear and month(ViewTime)=@tempmonth and day(ViewTime) = @tempday   if @tempyear!=0 AND @tempmonth =0 AND @tempyear!=0 
       return @err  if @tempyear!=0 AND @tempmonth !=0 AND @tempyear=0 
        select * from ViewLog where year(ViewTime)=@tempyear AND month(ViewTime)=@tempmonth   if @tempyear=0 AND @tempmonth !=0 AND @tempyear!=0 
        select * from ViewLog where year(ViewTime)=year(getdate()) AND month(ViewTime)=@tempmonth and day(ViewTime) = @tempday  if @tempyear=0 AND @tempmonth =0 AND @tempyear!=0 
        select * from ViewLog where  year(ViewTime)=year(getdate()) AND month(ViewTime)=month(getdate()) and day(ViewTime) = @tempday  if @tempyear=0 AND @tempmonth !=0 AND @tempyear=0 
        select * from ViewLog where  year(ViewTime)=year(getdate()) AND month(ViewTime)=@tempmonth  if @tempyear!=0 AND @tempmonth =0 AND @tempyear=0 
        select * from ViewLog where  year(ViewTime)=@tempyear 
    这是我写的存储过程,有点太笨了,想找个方便的方法
    上面是一个一个进行判断,,,还不能满足一个时间段的查询,谁有好点的方法啊
      

  5.   

    上面就是根据用户输入的参数(年月日)进行select查询,只不过用了很多if.
      

  6.   

    marco08()你的方法可行,那我想查一段时间内的数据,你再补充一下了,谢谢了
      

  7.   

    一段时间内的数据要用between ... and
    如果輸入2006, 則 between '2006-1-1' and '2006-12-31' 
    如果輸入200607 則 between '2006-07-01' and '2006-07-31'
      

  8.   

    也多谢dawugui(潇洒老乌龟)!!!