zoukankan      html  css  js  c++  java
  • (后端)sql server 按时间段查询

    百度的资料,保存下来:

    在写按时间段查询的sql语句的时候 一般我们会这么写查询条件:

      where date>='2010-01-01' and date<='2010-10-1'。

    但是在实执行Sql时些语句会转换成这样:

      where date>='2010-01-01 0:00:00' and date<='2010-10-1:0:00:00',再看这个条件的话,也许就会有些明白,

    那就是'2010-10-1 0:00:00' 之后的数据例如('2010-10-1:08:25:00')查不到,也就是说2010-10-1的数据查不到。

    知道原因了可以修改查询条件为:

       where date>='2010-01-01' and date<='2010-10-1 23:59:59' 或 where date>='2010-01-01' and date<='2010-10-2'。

    某个表某个字段是Datetime型 以"YYYY-MM-DD 00:00:00" 存放

     

    (1)、例如数据

    2009-01-22 21:22:22
    2009-01-22 19:21:11
    2009-01-22 23:10:22


    (2)、用 select * from TABLE where date between '2009-1-22' And '2009-1-22' ,想查日期为2009-1-22的记录,结果查不到

     

    (3)、问题原因

    短日期类型默认Time为00:00:00,所以当使用between作限制条件时,就相当于between '2009-1-22 00:00:00' and '2009-1-22 00:00:00',因此就查不出数据。

     

    (4)、解决方法



      --方案一:对数据库里面的字段进行日期格式转换

      select * from tb where convert(varchar(10),riqi,120) = '2009-01-22'


      --方案二:给日期补全时分秒

      select * from tb where riqi between '2009-01-22 00:00:00' and '2009-01-22 23:59:59'


      --结果
      /**//*
      id riqi
      ---- ------------------------------------------------------
      A 2009-01-22 21:22:22.000
      B 2009-01-22 19:21:11.000
      C 2009-01-22 23:10:22.000
      
      (所影响的行数为 3 行)
      */

  • 相关阅读:
    navicat连接虚拟机中mysql"Access denied for user'root'@'IP地址'"问题
    Centos6.4 + mysql-5.6.38-linux-glibc2.12-x86_64.tar 实现mysql主从复制
    三、mock测试技术
    二、数据加密
    一.unittest框架初识
    3.Allure报告
    2.pytest参数化
    1.pytest框架初识
    RabbitMQ 几种工作模式---(三) Publish/Subscribe
    RabbitMQ 几种工作模式---(二)work
  • 原文地址:https://www.cnblogs.com/historylyt/p/7638109.html
Copyright © 2011-2022 走看看