zoukankan      html  css  js  c++  java
  • SQL 时间条件查询

    to_date()使用

    select * from table t where t.time >= to_date(aaaa,'yyyy-mm-dd hh24:mm:ss') and t.time<to_date(bbbb,'yyyy-mm-dd hh24:mm:ss')
    

    aaaa,bbbb是字符串类型 比如:aaaa = '2018-04-19 00:00:00' bbbb = '2018-04-20 00:00:00'

    to_date()中yyyy-mm-dd hh24:mm:ss 意思把aaaa字符串转换成 yyyy-mm-dd hh24:mm:ss这样的时间格式

    select * from v$session where logon_time>=to_date('2018-04-19 00:00:00','yyyy-mm-dd hh24:mi:ss') and logon_time<to_date('2018-04-20 00:00:00','yyyy-mm-dd hh24:mi:ss');
    

    trunc(sysdate)使用

    select * from table t where t.time >= trunc(sysdate) and t.time < trunc(sysdate+1)
    

    sysdate是oracle数据库的系统当前时间 sysdate是时间格式的 trunc是oracle的截取函数 trunc(sysdate) 截取的结果是当前时间的yyyy-mm-dd 截取后也是如期类型

    select * from v$session where logon_time>=trunc(sysdate) and logon_time<trunc(sysdate+1);
    

    to_char()使用

    select * from table t where to_char(t.time,'yyyy-mm-dd hh24:mm:ss') >= aaaa and to_char(t.time,'yyyy-mm-dd hh24:mm:ss')<bbbb
    

    to_char()中t.time是表里的时间字段,先把t.time时间格式转换成字符串'yyyy-mm-dd hh24:mm:ss' 然后再和字符串aaaa,bbbb比较

    select * from v$session where to_char(logon_time,'yyyy-mm-dd hh24:mi:ss')>='2018-04-19 00:00:00' and to_char(logon_time,'yyyy-mm-dd hh24:mi:ss')<'2018-04-20 00:00:00';
    

    to_date()和to_char()的区别在于to_date()把查询条件字符串先转换成时间格式,to_char()把待查询的字段先转换成字符串然后再和查询条件字符串做比较

    select * from dual where to_char(sysdate, 'yyyy-MM-dd') = '2019-04-04';
    select * from dual where substr(sysdate, 0, 10) = to_date('2019-04-04', 'yyyy-MM-dd');//substr(sysdate,0,10)截取后仍为日期类型
    select * from dual where sysdate > to_date('2019-04-04', 'yyyy-MM-dd');
    
  • 相关阅读:
    springMVC学习笔记五(拦截器详解)
    springMVC学习笔记六(注解方式实现控制器)
    springMVC学习笔记四(数据类型转换和数据验证)
    springMVC学习笔记二(dispatcherServlet)
    springMVC学习笔记三(controller控制器)
    springMVC学习笔记一(hello world)
    mybatis学习笔记四(与spring3集成)
    扩展欧几里得————《数论》
    2017 多校赛 第二场
    POJ 2566 尺取法(进阶题)
  • 原文地址:https://www.cnblogs.com/xianyao/p/10847045.html
Copyright © 2011-2022 走看看