--测试环境
create table tiaa
(f1 nvarchar(10),
 ti_me smalldatetime)
insert into tiaa
select 'AA', '2005-12-15 09:55:24' union all
select 'AA', '2005-12-15 09:55:24' union all
select 'AA', '2005-12-15 09:55:24' union all
select 'AA', '2005-12-15 10:55:24' union all
select 'AA', '2005-12-15 10:55:24' union all
select 'AA', '2005-12-15 10:55:24' union all
select 'AA', '2005-12-15 10:55:24' union all
select 'AA', '2005-12-15 11:55:24' union all
select 'AA', '2005-12-15 11:55:24' union all
select 'AA', '2005-12-15 11:55:24' union all
select 'AA', '2005-12-15 11:55:24' 
go
--建立存储过程
create proc time_proc
@btime smalldatetime,
@etime smalldatetime
as
select * from tiaa where datepart(year,ti_me) = datepart(year,@btime)   and 
 datepart(month,ti_me) = datepart(month,@btime) and
 datepart(day,ti_me) = datepart(day,@btime)     and
 datepart(hour,ti_me) >= datepart(hour,@btime)   and 
 datepart(hour,ti_me) < datepart(hour,@etime)
go
--执行存储过程
exec time_proc '2005-12-15  9:00','2005-12-15 10:00'
exec time_proc '2005-12-15 10:00','2005-12-15 11:00'
exec time_proc '2005-12-15 11:00','2005-12-15 12:00'
--希望楼主以后不要起这样标题了。 有损自己的尊严!!!!!!!

解决方案 »

  1.   

    --测试数据
    create table #test(id char(2),dat datetime)
    insert into #test select 'AA','2005-12-15 09:55:24'
    insert into #test values('AA'  ,  '2005-12-15 09:55:24')
    insert into #test values('AA'  ,  '2005-12-15 09:55:24')
    insert into #test values('AA'  ,  '2005-12-15 10:55:24')
    insert into #test values('AA'  ,  '2005-12-15 10:55:24')
    insert into #test values('AA'  ,  '2005-12-15 10:55:24')
    insert into #test values('AA'  ,  '2005-12-15 10:55:24')
    insert into #test values('AA'  ,  '2005-12-15 11:55:24')
    insert into #test values('AA'  ,  '2005-12-15 11:55:24')
    insert into #test values('AA',    '2005-12-15 11:55:24')
    insert into #test values('AA',    '2005-12-15 11:55:24')
    --建函数
    create function f_time(@dat datetime)
    returns varchar(10)
    as 
    begin
    declare @char varchar(10)
    select @char=cast(datepart(hour,@dat) as varchar(10)) + '-' + cast((datepart(hour,@dat)+1) as varchar(10)) 
    return @char
    end
    --查询
    select dbo.f_time(dat) as id,dat from #test 
    --结果
    id         dat                                                    
    ---------- -------------------------------- 
    9-10       2005-12-15 09:55:24.000
    9-10       2005-12-15 09:55:24.000
    9-10       2005-12-15 09:55:24.000
    10-11      2005-12-15 10:55:24.000
    10-11      2005-12-15 10:55:24.000
    10-11      2005-12-15 10:55:24.000
    10-11      2005-12-15 10:55:24.000
    11-12      2005-12-15 11:55:24.000
    11-12      2005-12-15 11:55:24.000
    11-12      2005-12-15 11:55:24.000
    11-12      2005-12-15 11:55:24.000(所影响的行数为 11 行)