想求上月一号凌晨到最后一天24点的数据
不知道这样写对不对啊
高手指点迷津declare @tempmonth datetime
set @tempmonth  = left(dateadd(mm,-1,getdate()),7)select * from Month where between @tempmonth + '00:00:00' and @tempmonth + '23:59:00' 

解决方案 »

  1.   

    不要沉下去啊
    这个是小弟新写的
    高手帮忙看看阿declare @tempmonth datetime
    set @tempmonth  = left(dateadd(mm,-1,getdate()),7)select * from Month where Time between @tempmonth + '00:00:00' and @tempmonth + '23:59:00' 
      

  2.   

    有必要这么复杂么
    select * from month where year(time)*12+month = year(getdate())*12+month(getdate())-1
      

  3.   

    declare @starDate datetime --上月第一天
    declare @endDate datetime  --下月第一天
    declare @nowDate datetime  --本月第一天
    set @starDate=dateadd(month,-1,Convert(varchar(8),getdate(),120)+@statusDay)
    set @endDate=dateadd(month,1,Convert(varchar(8),getdate(),120)+@statusDay)
    set @nowDate=dateadd(month,0,Convert(varchar(8),getdate(),120)+@statusDay)有了这些值你就可以去判断了
      

  4.   

    @statusDay是什么意思啊?
    这个不是很清楚阿
      

  5.   

    兄弟新写的
    declare @last datetime,@this datetime
    set @last = left( convert(varchar(10),dateadd(mm,-1,getdate()),111),7)
    set @this = left(convert(varchar(10),getdate(),111),7)
    select * from  dbo.Tbl_Agent_Hour where StatisticsTime between @last+'00:00:00' and @this+'00:00:00' 总是说“从字符串转换为 datetime 时发生语法错误。”
      

  6.   

    @statusDay  是sqlserver 中的参数  不需要自己设置  可以直接使用
      

  7.   

    我执行declare @starDate datetime --上月第一天
    declare @endDate datetime  --下月第一天
    declare @nowDate datetime  --本月第一天
    set @starDate=dateadd(month,-1,Convert(varchar(8),getdate(),120)+@statusDay)
    set @endDate=dateadd(month,1,Convert(varchar(8),getdate(),120)+@statusDay)
    set @nowDate=dateadd(month,0,Convert(varchar(8),getdate(),120)+@statusDay)
    print @starDate
    print @endDate
    print @nowDate
    时说“必须声明变量 '@statusDay'。”
      

  8.   

    set @last = cast(left( convert(varchar(10),dateadd(mm,-1,getdate()),111),7)+'/01' as datetime)还可以,年月怎么转datetime
      

  9.   

    最后写的,效率可能比较差
    declare @last varchar(),@this varchar()
    set @templastmonth = left( convert(varchar(10),dateadd(mm,-1,getdate()),120),7)
    set @tempthismonth = left(convert(varchar(10),getdate(),120),7)selcet * from 数据表 where 查询时间 between cast(@templastmonth+'-01 00:00:00' as datetime) and  cast(@tempthismonth+'-01 00:00:00' as datetime) 
      

  10.   

    --求上月??select * from 數據表
    where year(查詢時間)=year(dateadd(mm,-1,getdate()))
    and month(查詢時間)=month(dateadd(mm,-1,getdate()))--或者
    select * from 數據表
    where 查詢時間>=convert(datetime,convert(char(07),dateadd(mm,-1,getdate()),120)+'-01')
    and 查詢時間<convert(datetime,convert(char(07),getdate(),120)+'-01')
      

  11.   

    这样的话,你起先的就用datetime不行吗?后面与前面转来转去的,不嫌麻烦吗?
      

  12.   

    select * from month where year(time)*12+month = year(getdate())*12+month(getdate())-1
    这句也可以啊
      

  13.   

    不过time要求是时间类型 datetime
      

  14.   

    declare @first varchar(10),@end varchar(10)
    set @first=convert(char(8),dateadd(month,-1,getdate()),120)+'1'
    set @end=convert(char(8),getdate(),120)+'1'
    select @first--上个月第一天
    select @end--上个月的最后一天