create procedure test @day int
As
Select * from 
(Select kcode,max(op_date) as op_date from 表 group by kcode) aa
where datediff(day,op_date,getdate()) >= @day
go/****** Usage:
Exec test 27
*/

解决方案 »

  1.   

    select * from 表 where datediff(day,op_date,getdate())=2
      

  2.   

    如:select datediff(day,'2001-1-1','2002-1-1') 天
    select datediff(month'2001-1-1','2002-1-1') 月
    select datediff(year,'2001-1-1','2002-1-1') 年
      

  3.   

    DATEDIFF
    返回跨两个指定日期的日期和时间边界数。 语法
    DATEDIFF ( datepart , startdate , enddate ) 参数
    datepart是规定了应在日期的哪一部分计算差额的参数。下表列出了 Microsoft® SQL Server™ 识别的日期部分和缩写。日期部分 缩写 
    year yy, yyyy 
    quarter qq, q 
    Month mm, m 
    dayofyear dy, y 
    Day dd, d 
    Week wk, ww 
    Hour hh 
    minute mi, n 
    second ss, s 
    millisecond ms 
    startdate是计算的开始日期。startdate 是返回 datetime 或 smalldatetime 值或日期格式字符串的表达式。 因为 smalldatetime 只精确到分钟,所以当用 smalldatetime 值时,秒和毫秒总是 0。如果您只指定年份的最后两位数字,则小于或等于"两位数年份截止期"配置选项的值的最后两位数字的数字所在世纪与截止年所在世纪相同。大于该选项的值的最后两位数字的数字所在世纪为截止年所在世纪的前一个世纪。例如,如果 two digit year cutoff 为 2049(默认),则 49 被解释为 2049,2050 被解释为 1950。为避免模糊,请使用四位数的年份。有关时间值指定的更多信息,请参见时间格式。有关日期指定的更多信息,请参见 datetime 和 smalldatetime。 enddate是计算的终止日期。enddate 是返回 datetime 或 smalldatetime 值或日期格式字符串的表达式。返回类型
    integer注释
    startdate 是从 enddate 减去。如果 startdate 比 enddate 晚,返回负值。当结果超出整数值范围,DATEDIFF 产生错误。对于毫秒,最大数是 24 天 20 小时 31 分钟零 23.647 秒。对于秒,最大数是 68 年。计算跨分钟、秒和毫秒这些边界的方法,使得 DATEDIFF 给出的结果在全部数据类型中是一致的。结果是带正负号的整数值,其等于跨第一个和第二个日期间的 datepart 边界数。例如,在 1 月 4 日(星期日)和 1 月 11 日(星期日)之间的星期数是 1。示例
    此示例确定在 pubs 数据库中标题发布日期和当前日期间的天数。USE pubs
    GO
    SELECT DATEDIFF(day, pubdate, getdate()) AS no_of_days
    FROM titles
    GO
      

  4.   

    谢谢  txlicenhe(马可) !!
    按你的方法试了一下:如果 记录中kcode不相同的话可以准确的查询出来,但记录中有相同的kcode时就查不到数据了。
    如 kcode   op_date
    ----------------------------
    001         2003-09-15 14:38:14.000
    001         2003-09-28 15:38:14.000
    001         2003-10-15 15:38:14.000以今天时间 2003-10-18 算,按我想要的结果应该是(只关注kcode的日期和当天时间的时间差)如 kcode   op_date
    ----------------------------
    001         2003-10-15 15:38:14.000但你的方法现在还查不到。请再帮帮忙!!!感谢!
      

  5.   

    to  pengdali(大力 V3.0)  
    谢谢你的参与,我不是仅仅查询最近几天的记录呀!!!要的是 kcode(客户) 最后一次op_date来店的时间差的总报表。可能是我开始没表达清楚吧。你的关注是我的荣幸!
      

  6.   

    create procedure test 
    @day int
    As
    select * from 表 
    where datediff(day,op_date,getdate())=@day--run
    execute test 2
      

  7.   

    create table 表 (kcode varchar(10),op_date datetime)
    insert 表 select '001','2003-09-15 14:38:14.000'
    union all select '001','2003-09-28 15:38:14.000'
    union all select '001','2003-10-15 15:38:14.000'楼主的意思是要输入两个参数吧?
    create procedure test @day int,@rq datetime
    As
    Select * from 
    (Select kcode,max(op_date) as op_date from 表 group by kcode) aa
    where datediff(day,op_date,@rq) >= @day
    go/****** Usage:
    Exec test 2,'2003-10-18'   kcode      op_date                                                
    ---------- ------------------------------------------------------ 
    001        2003-10-15 15:38:14.000(所影响的行数为 1 行)
    */
      

  8.   

    to 马可: 
    你上面的方法的确可行,但是如果改成如下:
    create table 表 (kcode varchar(10),op_date datetime)
    insert 表 select '001','2003-09-15 14:38:14.000'
    union all select '001','2003-09-28 15:38:14.000'
    union all select '002','2003-09-28 15:38:14.000'
    union all select '001','2003-10-15 15:38:14.000'Select * from 
    (Select kcode,max(op_date) as op_date from 表 group by kcode) aa
    where datediff(day,op_date,'2003-10-18') >= 2得到的结果如下:
    kcode      op_date                                                
    ---------- ------------------------------------------------------ 
    001        2003-10-15 15:38:14.000
    002        2003-09-28 15:38:14.000  (已经有20天没有记录了,所以不应该被查询出来)上面的参数是2 (查询最近2天没有记录的kcode), 而 002 的日期是 2003-09-28 ,所以不应该在记录集里。
      

  9.   

    declare @no_vst int
    set @no_vst =23
    select * from [your table] where
       (select count(*) from [your table]  t where 
         abs(t.op_date-[your table].op_date)        --用绝对值批判断 相差的日期
             <=@no_vst)=0
      

  10.   

    declare @no_vst int
    set @no_vst =23
    select * from [your table] where
       (select count(*) from [your table]  t where t.kcode=[your table].kcode and
         abs(t.op_date-[your table].op_date)        --用绝对值批判断 相差的日期
             <=@no_vst)=0
    老写错。。郁闷
      

  11.   

    to realgz 报错如下:
    Server: Msg 257, Level 16, State 3, Line 3
    Implicit conversion from data type datetime to float is not allowed. Use the CONVERT function to run this query.咱回事呀?请再看看
      

  12.   

    转换不过来 ?那强制转换。
    declare @no_vst int
    set @no_vst =23
    select * from [your table] where
       (select count(*) from [your table]  t where t.kcode=[your table].kcode and
         abs(convert(float,t.op_date-[your table].op_date))        --用绝对值批判断 相差的日期
             <=@no_vst)=0