查询2021-01-01 至 2021-01- 02 的数据
SELECT *
FROM t_table1 t
WHERE t.d_time >= to_date('2021-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
AND t.d_time <= to_date('2021-01-02 23:59:59', 'yyyy-mm-dd hh24:mi:ss');
以下SQL,只会查询2021-01-01至2021-1-2 00:00:00的数据
SELECT *
FROM T_EVENT_MANAGEMENT t
WHERE t.s_ra_time >= to_date('2021-01-01', 'yyyy-mm-dd')
AND t.s_ra_time <= to_date('2021-01-02', 'yyyy-mm-dd');
--to_date('2021-01-02', 'yyyy-mm-dd') = 2021-01-02 00:00:00 超过2号0点属于2号的数据不会显示
2. 日期和字符转换函数用法(to_date,to_char)
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; --结果:2022-01-26 13:04:53
select to_char(sysdate,'yyyy') as nowYear from dual; --结果:2022
select to_char(sysdate,'mm') as nowMonth from dual; --结果:01
select to_char(sysdate,'dd') as nowDay from dual; --结果:26
select to_char(sysdate,'hh24') as nowHour from dual; --结果:13
select to_char(sysdate,'mi') as nowMinute from dual; --结果:04
select to_char(sysdate,'ss') as nowSecond from dual; --结果:53
select to_date('2022-01-26 13:04:53','yyyy-mm-dd hh24:mi:ss') from dual
3. 查询某天星期几
select to_char(to_date('2022-01-26','yyyy-mm-dd'),'day') from dual; --结果:星期三
4. 两个日期直接相差天数
select floor(sysdate - to_date('20220101','yyyymmdd')) from dual;
5. 查询出一个空的时间类型
select 1, TO_DATE(null) from dual;
6. 用于计算date1和date2之间有几个月
select months_between(to_date('12-31-2021','MM-DD-YYYY'),to_date('01-31-2021','MM-DD-YYYY')) "MONTHS" FROM DUAL; --结果:11
7. 指定时间的下一个星期几(由char指定)所在的日期,
NEXT_DAY(date,char)
select next_day(sysdate,2) from dual; --当前时间的下一个周一
--1表示星期日,2代表星期一
select sysdate S1,
trunc(sysdate) S2, //返回当前日期,无时分秒
trunc(sysdate,'year') YEAR, //返回当前年的1月1日,无时分秒
trunc(sysdate,'month') MONTH , //返回当前月的1日,无时分秒
trunc(sysdate,'day') DAY //返回当前星期的星期天,无时分秒
from dual
10. 返回日期列表中最晚日期
select greatest('2021-01-04','2022-01-04','2019-02-04') from dual; --结果:2022-01-04
11. 计算时间差
select floor(to_number(sysdate-to_date('2020-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from dual //时间差-年
select ceil(months_between(sysdate,to_date('2020-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanMonths from dual //时间差-月
select floor(to_number(sysdate-to_date('2020-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanDays from dual //时间差-天
select floor(to_number(sysdate-to_date('2020-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from dual //时间差-时
select floor(to_number(sysdate-to_date('2020-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60) as spanMinutes from dual //时间差-分
select floor(to_number(sysdate-to_date('2020-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60*60) as spanSeconds from dual //时间差-秒