不知道那个人设计的这个表,真想扁他数据库中有一个记录表
记录表中的年,月,日,时,分,秒分开存储现在要用java实现
查询4个小时前到当前时间的记录表中的数据

2011-12-29 17:30:30 ~ 2011-12-29 13:30:30
另一种
2011-12-28 22:30:30 ~ 2011-12-29 2:30:30
快崩溃了,大侠救命

解决方案 »

  1.   

    你放一起拼个日期类型
    where convert(datetime,ltrim(yy)+'-'+right(100+mm,2)+'-'+right(100+dd,2)+' '
     +right(100+hh,2)+':'+right(100+mi,2)+':'+right(100+ss,2))
     between '2011-12-29 17:30:30' and '2011-12-29 13:30:30'
      

  2.   

    select * from tb where ltrim(年)+'-'+ltrim(月)+'-'+ltrim(日)+' '+ltrim(时)+':00:00' between dateadd(hh,-4,getdate()) and getdate()
      

  3.   

    --拼接起来再查询select t.* , 
           cast(年 as varchar) + '-' +
           cast(月 as varchar) + '-' +
           cast(日 as varchar) + '-' + ' ' + 
           cast(时 as varchar) + ':' +
           cast(分 as varchar) + ':' +
           cast(秒 as varchar)
    from tb where cast(
           cast(年 as varchar) + '-' +
           cast(月 as varchar) + '-' +
           cast(日 as varchar) + '-' + ' ' + 
           cast(时 as varchar) + ':' +
           cast(分 as varchar) + ':' +
           cast(秒 as varchar)
     as datetime) between dateadd(hh,-4,getdate()) and getdate()
      

  4.   

    这个也不行,报错,从char数据类型到datetime数据类型的转换导致datetime值越界
      

  5.   


    要考虑分钟就加一个呗!
    select * 
    from tb 
    where ltrim(年)+'-'+ltrim(月)+'-'+ltrim(日)+' '+ltrim(时)+ltrim(分)+':00' between dateadd(mi,-240,getdate()) and getdate()如果要考虑秒再可以再加上去.写一个程序给你,你要自己动点脑筋的,别人不太可能与你想得严丝合缝一点没差别.
      

  6.   

    lz淡定。。
    别人那样设计数据库肯定有他的想法。。
    或许是为了以后的某些功能才那样设计的。。先别想这个。。
    lz试试这个方法。是否满意。。-- =============================================
    -- Author: <chenlong>
    -- Create date: <2011-12-30 11:15:20>
    -- Description: <判断传入年月日时分秒与当前时间是否相差n小时>
    -- =============================================
    create proc PROC_TestDateTime
    (@year varchar(4),@month varchar(2),@day varchar(2),@hour varchar(2),@minute varchar(2),@second varchar(2))
    as
    begin
    declare @DiffTime int
    set @DiffTime=4
    declare @DateTime varchar(20)
    set @DateTime=@year+'-'+@month+'-'+@day+' '+@hour+':'+@minute+':'+@second
    if datediff(hour,convert(smalldatetime,@DateTime),getdate())>@DiffTime
    begin
    print 'true'
    end
    else
    begin
    print 'false'
    end
    end