SQL数据库中记录日期格式的数据如 ‘2012-8-13 00:00:00’假如1小时一条数据 一天24小时 怎么提取当天的数据?select * from rec_pm where date = CONVERT(varchar(100), GETDATE(), 23)为什么没有数据啊?

解决方案 »

  1.   

    =号两们的时间格式要相同,
    比较“年-月-日”相等就可以了,如下
    select * 
    from rec_pm 
    where CONVERT(varchar(12) ,date , 111 )=CONVERT(varchar(12) , getdate(), 111 )
      

  2.   

    我之前也遇上取不出数据的情况,我是用dateTimePicker取当天的日期,你用其他方式也可以去到当天的日期
    string kssj =dateTimePicker1.Value.ToString().Substring(0,dateTimePicker1.Value.ToString().IndexOf(" ")) + " 00:00:00";
    string jssj = dateTimePicker2.Value.ToString().Substring(0, dateTimePicker2.Value.ToString().IndexOf(" ")) + " 23:59:59";以上字符我是获取当天的日期然后将时间转换一下,比如当天日期是2012-8-10,我加上时间变成2012-8-10 00:00:00,和结束时间2012-8-10 23:59:59
    然后用查询语句,假设你时间的字段名就叫“时间”
    select * from rec_pm where date 时间>=kssj and 时间<=jssj
    你试试吧,有问题再交流
      

  3.   


    SELECT  
    SUBSTRING(CONVERT(VARCHAR,date,120),0,14) AS '时间',
    COUNT(1) AS '数量'  
    from rec_pm  
    where CONVERT(varchar(12) ,date , 111 )=CONVERT(varchar(12) , getdate(), 111 )
    GROUP BY  
    SUBSTRING(CONVERT(VARCHAR,date,120),0,14)
      

  4.   

    SELECT COUNT(*)FROM rec_pm  WHERE datediff(HH,date,GETDATE())=0
      

  5.   


    查询当天的记录 
    select * from tableName where DATEPART(dd, theDate)=DATEPART(dd, GETDATE()) and
    DATEPART(mm, theDate)=DATEPART(mm, GETDATE()) and
    DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
    查询本月的记录 
    select * from tableName where DATEPART(mm, theDate)=DATEPART(mm, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE()) 
    查询本周的记录 
    select * from tableName where DATEPART(wk, theDate) = DATEPART(wk, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE()) 
    查询本季的记录 注:其中:GETDATE()是获得系统时间的函数。 
    select * from tableName where DATEPART(qq, theDate) = DATEPART(qq, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
      

  6.   


    select datepart(hh,date)
    ,count(1) 分小时数量
    --如果有求和的,此处加sum(求和列)
     from rec_pm 
    where convert(varchar(8),date,112 )=convert(varchar(8),getdate(),112)
    group by datepart(hh,date)
      

  7.   

    查询的时间要相同
    sql += " [CommTime] between '" + st + "'and '" + et + "'";
    其中st和et是起始时间跟截止时间