yyyy-MM-dd HH:mm:ss格式时间作为sql查询条件

发布时间 2023-04-24 16:02:37作者: 尔本

年月日范围查询
select t.id from tablename t where t.create >=to_date('"+startTime+"','YYYY-MM-DD') and t.create<=to_date('"+endTime+"','YYYY-MM-DD');

select t.id from tablename t where t.create between to_date('"+startTime+"','YYYY-MM-DD') and to_date('"+endTime+"','YYYY-MM-DD');
精确到时分秒范围查询
select t.id from tablename t where t.create>=to_date('"+startDate+" "+startTime+"','YYYY-MM-DD hh24:mi:ss') and t.create <to_date('"+ startDate + " "+endTime+"','YYYY-MM-DD hh24:mi:ss');
日期类型create也可以转为字符类型进行精确查询
select t.id from tablename t where to_char(t.create,'YYYY-MM-DD')='"+startTime+"';

注:oracle不区分大小写,因此分钟使用mi
(踩了许多坑)