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');

原文地址:https://www.cnblogs.com/xianyao/p/10847045.html

时间: 2024-07-29 18:05:52

SQL 时间条件查询的相关文章

Linq to Sql 多条件查询

Linq To Sql 多条件查询 string proName = this.txtName.Text.Trim();string lowPrice = this.txtLowPrice.Text.Trim();string highPrice = this.txtHighPrice.Text.Trim(); decimal? lowPrice1 = null, highPrice1 = null;if (!string.IsNullOrEmpty(lowPrice)){        low

C# SQL多条件查询拼接技巧

本文转载:http://blog.csdn.net/limlimlim/article/details/8638080 #region 多条件搜索时,使用List集合来拼接条件(拼接Sql) StringBuilder sql = new StringBuilder("select * from PhoneNum"); List<string> wheres = new List<string>(); if (cboGroup.SelectedIndex !=

SQL多条件查询安全高效比较

ALTER PROCEDURE _tmp @ID VARCHAR(50), @PN VARCHAR(50), @Type INT AS BEGIN /********************************** -- 功能:多条件查询性能 _tmp 'K3G8KG6NN94SBBS0','K7F7FF',0 **********************************/ PRINT '测试数据条数500W' set nocount ON DECLARE @time DATETIM

Webform中linq to sql多条件查询(小练习)

多条件查询:逐条判断,从第一个条件开始判断,如果满足,取出放入集合,再从集合中查询第二个条件... aspx代码: 1 <body> 2 <form id="form1" runat="server"> 3 4 <br /> 5 <asp:Label ID="Label1" runat="server" Text="关键字:"></asp:Label&g

qt sql多重条件查询简便方法

转载请注明出处:http://www.cnblogs.com/dachen408/p/7457312.html 程序设计过程中,经常要涉及到查询,并且有很多条件,且条件可为空,如果逐个判断,会有很多情况,解决方案: QSqlQuery query(m_db); QString province = QString::fromLocal8Bit("广东"); QString city = ""; QString null = ""; QString

SQL 多条件查询

网上有不少人提出过类似的问题:“看到有人写了WHERE 1=1这样的SQL,到底是什么意思?”.其实使用这种用法的开发人员一般都是在使用动态组装的SQL.让我们想像如下的场景:用户要求提供一个灵活的查询界面来根据各种复杂的条件来查询员工信息,界面如下图: 界面中列出了四个查询条件,包括按工号查询.按姓名查询.按年龄查询以及按工资查询,每个查询条件前都有一个复选框,如果复选框被选中,则表示将其做为一个过滤条件.比如上图就表示“检索工号介于DEV001和DEV008之间.姓名中含有J并且工资介于30

mysql按照时间条件查询 忽略是时分秒查询

数据库保存的是2018-11-06 12:22:23这种格式数据然后页面查询条件是>=2018-11-06和<=2018-11-06,想把6号的数据都查询出来但是因为有时分秒导致<=2018-11-06不成立:所以查不出数据1:格式化数据库日期date_format(b.CREATE_TIME,'%Y-%m-%d00:00:00') AS datetime,2:格式化查询条件cq.ge("dateTime", new SimpleDateFormat("yy

C# SQL 多条件查询技巧

#region 多条件搜索时,使用List集合来拼接条件(拼接Sql) StringBuilder sql = new StringBuilder("select * from PhoneNum");            List<string> wheres = new List<string>();            if (cboGroup.SelectedIndex != 0)            {                wheres.

sql 多条件查询 拼接字符串 改成 普通查询格式

set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER PROC [dbo].[usp_SRV_CheckServiceDemandOrder] @AInsNO NVARCHAR(50) =null,--必填 @ACompanyName NVARCHAR(50) = null,--必填 @ADepartmentName NVARCHAR(50) = null, @AName NVARCHAR(50) = null, --必填 @ApplicantI