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

    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');
  • 相关阅读:
    windows 查看某个端口号被占用情况
    C# 配置文件ini操作类
    C#:如何解决WebBrowser.DocumentCompleted事件的多次调用
    什么是异或_异或运算及异或运算的作用
    UID卡、CUID卡、FUID卡的区别
    C#获取窗口大小和位置坐标 GetWindowRect用法
    C#中SetWindowPos函数详解
    C#让电脑发声,播放声音
    C#自动缩进排列代码的快捷键 c# 代码重新排版 变整齐
    安卓手机USB无法共享、上网或卡顿的解决方法
  • 原文地址:https://www.cnblogs.com/ms-grf/p/8881397.html
Copyright © 2011-2022 走看看