您的位置:程序门 -> ms-sql server -> 应用实例



mssql 里怎么查询出一段时间内的内容


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


mssql 里怎么查询出一段时间内的内容[已结贴,结贴人:yugy82]
发表于:2007-03-29 16:00:52 楼主
我想查询数据库里最近一个月的数据,但不知道sql查询语句该怎么写
那个打下指点一下小弟!!
发表于:2007-03-29 16:05:321楼 得分:15

--这里说的一个月,是按天?

select   *   from   表名
where   datediff(d,日期字段名,getdate()) <30
发表于:2007-03-29 16:19:062楼 得分:2
declare   @getcurrentdate   datetime
set   @getcurrentdate   =   getdate()
select   *
from   表
where   datediff(dd,@getcurrentdate,表中的日期字段)   > =   30
说明:“表中的日期字段”也须是datetime类型。
发表于:2007-03-29 16:20:253楼 得分:1
不好意思,是
“ <=   30”

更正:
declare   @getcurrentdate   datetime
set   @getcurrentdate   =   getdate()
select   *
from   表
where   datediff(dd,@getcurrentdate,表中的日期字段)   <=   30
说明:“表中的日期字段”也须是datetime类型。
发表于:2007-03-29 23:50:274楼 得分:1
如是上一个月的数据就用:
select   *   from   表名
where   日期字段名   between   dateadd(dd,1-day(dateadd(dd,-day(getdate()),getdate())),dateadd(dd,-day(getdate()),getdate()))   and   dateadd(dd,-day(getdate()),getdate())
发表于:2007-03-30 14:53:245楼 得分:1
create   proc   存储过程名


      @startdate   numeric(8,0), ---开始时间
      @enddate   numeric(8,0)                           --结束时间
)
as  
declare   @sql   varchar(5000)
declare   @subwhere   varchar(2000)


set   @sql= ' '
set   @subwhere= ' '

if   @startdate <> 0   and   @startdate   is   not   null
set   @subwhere=@subwhere+ 'and   n_date> = '+convert(varchar(15),@startdate)

if   @enddate <> 0   and   @enddate   is   not   null
set   @subwhere=@subwhere+ 'and   n_date <= '+convert(varchar(15),@enddate)


set   @sql= 'select   *
                                from  

where   1=1   '+@subwhere


EXEC(@sql)


go
这个比较灵活,可以查任何时间段的
发表于:2007-07-01 13:04:256楼 得分:0
请问这个存储过程应该怎么调用呢,结果输出是什么?
发表于:2007-07-04 15:56:597楼 得分:0
select   *
from  

where   1=1   '+@subwhere

这个就是结果
发表于:2007-07-16 15:04:148楼 得分:0
汗,难道不是time   between   日期1   and   日期2  

么?
发表于:2007-07-16 16:22:349楼 得分:0
可以直接
select   *   from   table   where   date < '2007-7-1 '   and   date> '1900-1-1 '
或者
select   *   from   table   where   date   between   '2007-7-1 '   and   date> '1900-1-1 '


快速检索

最新资讯
热门点击