username          uploadtime   uNum
系统用户          2009-6     11
系统用户          2009-8     1
系统用户          2009-9     1如上,
select*from V_UploadMonth where UploadTime >'2009-5' and UploadTime<'2009-10'
这个语句查不到....?

解决方案 »

  1.   

    uploadtime  是datetime类型还是varchar类型?
      

  2.   

    字符串比较....修改你的uploadtime  为 2009-01  2009-12 这样吧
      

  3.   

    你要说清楚是什么数据库,还有uploadtime  是什么类型呀
      

  4.   

    我是查询一个视图:
    SELECT     dbo.[User].UserName, CONVERT(varchar, YEAR(dbo.Upload.UploadTime)) + '-' + CONVERT(varchar, MONTH(dbo.Upload.UploadTime)) AS UploadTime, 
                          COUNT(*) AS uNum
    FROM         dbo.Upload INNER JOIN
                          dbo.[User] ON dbo.[User].ID = dbo.Upload.UploadUserID
    WHERE     (dbo.[User].RoleID = 1) AND (dbo.Upload.Deleted = 0)
    GROUP BY dbo.[User].UserName, CONVERT(varchar, YEAR(dbo.Upload.UploadTime)) + '-' + CONVERT(varchar, MONTH(dbo.Upload.UploadTime))
      

  5.   

    判断日期大小:用datediff(type,start,end),sql函数。这样怎么都不会有错。!!!
      

  6.   

     CONVERT(varchar, YEAR(dbo.Upload.UploadTime)) + '-' + CONVERT(varchar, MONTH(dbo.Upload.UploadTime)) AS UploadTime,换成
    CONVERT(varchar(7), UploadTime, 23)
      

  7.   


    要不然你就用datetime类型
      

  8.   

    插入uploadtime字段时,最好格式化一下:ToString("yyyy-MM")。这样就不会有问题了.
      

  9.   

    select * from V_UploadMonth where UploadTime+'-1'>'2009-5-1' and UploadTime+'1' <'2009-10-1' try
      

  10.   

    我如何优化那个视图才能用
    select*from V_UploadMonth where UploadTime >'2009-5' and UploadTime <'2009-10' 
    查询到呢,因为我查询的条件只需要精确到月,也就是YYYY-MM就够了
      

  11.   

    select*from V_UploadMonth where UploadTime >'''2009-5''' and UploadTime <'''2009-10''' 
    时间型的这样处理
      

  12.   

    select *
    from V_UploadMonth
    where UploadTime between '2009-6' and '2009-9'
      

  13.   

    用between ....and ...
    select * from V_UploadMonth where  UploadTime between '2009-5' and '2009-10' 
      

  14.   


    如果是6月到9月,23楼的就可以,如果是3月到11月,就要这样:
    select *
    from V_UploadMonth
    where (UploadTime between '2009-3' and '2009-9')
      or UploadTime in ('2009-10','2009-11')
      

  15.   

    select *
    from V_UploadMonth
    where UploadTime in ('2009-6','2009-7','2009-8','2009-9')
      

  16.   


    declare @t1 table(username varchar(20),uploadtime varchar(20),uNum int)
    insert into @t1 values('系统用户','2009-6',11) 
    insert into @t1 values('系统用户','2009-8',1) 
    insert into @t1 values('系统用户','2009-9',1) 
    select * from @t1 where Convert(datetime,UploadTime+'-01') >Convert(datetime,'2009-5'+'-01')
     and Convert(datetime,UploadTime+'-01') <Convert(datetime,'2009-10'+'-01' )
      

  17.   

    恩,楼上几位给了我几个灵感,继续TRY
      

  18.   

    你这个视图不能这样写查出的时间 不用转换成 varchar , 想精度到月 可以 在 where 后面做操作的
      

  19.   


    UploadTimebetween'2009-3'and'2009-9'改成UploadTimebetween'2009-3'and'2009-12'就不行了
      

  20.   

    将日期格式成字符串保存在varchar,还要使用日期比较查询,你这样做明摆着是找麻烦,无趣
      

  21.   

    字符串,是一个一个ASCII进行比较的
    “2009-5”与“2009-10”,前面的“2009-”都是一样的,后面的“5”>“1”,所以找不到
      

  22.   

    我定义了一个User表和Upload表,
    V_UploadMonth这个视图是用来提取User里面的UserName,Upload里面的UploadTime
    User表的结构大同小异,Upload的UploadTime类型是datetime
      

  23.   


    declare @t table(username  varchar(20),uploadtime datetime,uNum int)insert into @t select '系统用户','2009-06-29 08:16:18.907',5
    union all select '系统用户',         '2009-06-29 08:30:08.620',     3
    union all select '系统用户',         '2009-06-30 12:00:55.917',     3
    union all select '系统用户',         '2009-08-31 14:35:10.327',     1
    union all select '系统用户',         '2009-09-02 10:00:16.787',     1select * from @t select min(username) username,cast(YEAR(uploadtime) as varchar(4))+'-'+cast(month(uploadtime) as varchar(2)) as [date],sum(uNum) uNum from @t 
    where uploadtime>'2009-05-01' and uploadtime<'2009-10-01'
    group by cast(YEAR(uploadtime) as varchar(4))+'-'+cast(month(uploadtime) as varchar(2))
      

  24.   


    declare @t1 table(username varchar(20),uploadtime varchar(20),uNum int)
    insert into @t1 values('系统用户','2009-6',11) 
    insert into @t1 values('系统用户','2009-8',1) 
    insert into @t1 values('系统用户','2009-9',1) select *  from @t1 
    where Convert(int,REPLACE( uploadtime,'-','')) between 
    Convert(int,REPLACE( '2009-5','-','')) and 
    Convert(int,REPLACE( '2009-10','-',''))
      

  25.   

    你的表结构是这样的吧username uploadtime                     uNum
    系统用户 2009-06-29 08:16:18.907              5
    系统用户 2009-06-29 08:30:08.620              3
    系统用户 2009-06-30 12:00:55.917              3
    系统用户 2009-08-31 14:35:10.327              1
    系统用户 2009-09-02 10:00:16.787              1
      

  26.   

    declare @minvalue varchar(100)
    declare @maxvalue varchar(100)
    set @minvalue='2009-5'
    set @maxvalue='2009-10'
    select*from V_UploadMonth where 
    cast(uploadtime+'-1' as datetime) between cast(@minvalue+'-1' as datetime) and cast (@maxvalue+'-1' as datetime)
      

  27.   

    select min(username) username,cast(YEAR(uploadtime) as varchar(4))+'-'+cast(month(uploadtime) as varchar(2)) as [date],sum(uNum) uNum from @t 
    where uploadtime>'2009-05-01' and uploadtime<'2009-10-01'
    group by cast(YEAR(uploadtime) as varchar(4))+'-'+cast(month(uploadtime) as varchar(2))group by cast(YEAR(uploadtime) as varchar(4))+'-'+cast(month(uploadtime) as varchar(2))
    这句 就是根据 记录的年月分组
      

  28.   

    declare @t1 table(username varchar(20),uploadtime varchar(20),uNum int)
    insert into @t1 values('系统用户','2009-6',11) 
    insert into @t1 values('系统用户','2009-8',1) 
    insert into @t1 values('系统用户','2009-9',1) 那这个是什么意思呢?
      

  29.   

    查询的时候有这个:
    Must declare the table variable "@t1".
      

  30.   

    很简单,你把字符串格式化为'yyyy-mm',也就是'2009-05'和'2009-10'比较,就不会出错了。
      

  31.   


    declare @t1 table(username varchar(20),uploadtime datetime,uNum int)
    insert into @t1 values('系统用户' ,'2009-06-29 08:16:18.907', 5 )
    insert into @t1 values('系统用户' ,'2009-06-29 08:30:08.620', 3 )
    insert into @t1 values('系统用户' ,'2009-06-30 12:00:55.917', 3 )
    insert into @t1 values('系统用户' ,'2009-08-31 14:35:10.327', 1 )
    insert into @t1 values('系统用户' ,'2009-09-02 10:00:16.787', 1 )
    select LEFT(Convert(varchar(20),uploadtime,120),7) as uploadtime,sum(uNum) as uNum from @t1group by LEFT(Convert(varchar(20),uploadtime,120),7)
      

  32.   


    select min(username) username,
    cast(YEAR(uploadtime) as varchar(4))+'-'+cast(month(uploadtime) as varchar(2)) as [date],
    sum(uNum) uNum from @t 
    where uploadtime>'2009-05-01' and uploadtime<'2009-10-01'
    group by cast(YEAR(uploadtime) as varchar(4))+'-'+cast(month(uploadtime) as varchar(2))@t  换成你的表名
      

  33.   


    select Convert(varchar(7),uploadtime,120) as uploadtime,sum(uNum) as uNum from @t1group by Convert(varchar(7),uploadtime,120)或者指定长度为7就行了
      

  34.   

    头好大..越看你们写的头越晕..
    SQLSERVER博大精深啊
      

  35.   

    declare @a table(username  varchar(20),uploadtime datetime,uNum int)insert into @a select '系统用户','2009-06-29 08:16:18.907',5
    union all select '系统用户',         '2009-06-29 08:30:08.620',     3
    union all select '系统用户',         '2009-06-30 12:00:55.917',     3
    union all select '系统用户',         '2009-08-31 14:35:10.327',     1
    union all select '系统用户',         '2009-09-02 10:00:16.787',     1select * from @a select min(username) username,cast(YEAR(uploadtime) as varchar(4))+'-'+cast(month(uploadtime) as varchar(2)) as [date],sum(uNum) uNum from @t 
    where uploadtime>'2009-05-01' and uploadtime<'2009-10-01'
    group by cast(YEAR(uploadtime) as varchar(4))+'-'+cast(month(uploadtime) as varchar(2))看看这个!