Table of Contents
1. Query data within a time period
2. Date and character conversion function usage (to_date,to_char)
3. Query the day of the week for a certain day
4. The direct difference in days between two dates
5. Query an empty time type
6. Used to calculate the number of months between date1 and date2
7. The date of the next day of the week (specified by char) at the specified time,
8. Get the number of days in this year
9. Get the number of days in this year that the current time is
10. Return the latest date in the date list
11. Calculate the time difference
12. Find the first and last day of the month
13. Query the time before the time (replace the minus sign with a plus sign after checking)
Home Database Oracle Summary of some common situations of Oracle query based on time

Summary of some common situations of Oracle query based on time

Sep 02, 2022 pm 05:04 PM
oracle

This article brings you relevant knowledge about Oracle. Querying based on time is a function we often encounter in daily development. Here are some common queries about Oracle based on time. The situation is introduced in detail through example code in the article. Friends in need can refer to it.

Summary of some common situations of Oracle query based on time

Recommended tutorial: "Oracle Video Tutorial"

1. Query data within a time period

查询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(&#39;2021-01-02 23:59:59&#39;, &#39;yyyy-mm-dd hh24:mi:ss&#39;);
Copy after login

The following SQL will only query data from 2021-01-01 to 2021-1-2 00:00:00

SELECT *
FROM T_EVENT_MANAGEMENT t
WHERE t.s_ra_time >= to_date(&#39;2021-01-01&#39;, &#39;yyyy-mm-dd&#39;)
	AND t.s_ra_time <= to_date(&#39;2021-01-02&#39;, &#39;yyyy-mm-dd&#39;);
--to_date(&#39;2021-01-02&#39;, &#39;yyyy-mm-dd&#39;) = 2021-01-02 00:00:00 超过2号0点属于2号的数据不会显示
Copy after login

2. Date and character conversion function usage (to_date,to_char)

select to_char(sysdate,&#39;yyyy-mm-dd hh24:mi:ss&#39;) as nowTime from dual;  --结果:2022-01-26 13:04:53
select to_char(sysdate,&#39;yyyy&#39;) as nowYear   from dual; --结果:2022
select to_char(sysdate,&#39;mm&#39;)   as nowMonth  from dual; --结果:01
select to_char(sysdate,&#39;dd&#39;)   as nowDay    from dual; --结果:26
select to_char(sysdate,&#39;hh24&#39;) as nowHour   from dual; --结果:13
select to_char(sysdate,&#39;mi&#39;)   as nowMinute from dual; --结果:04
select to_char(sysdate,&#39;ss&#39;)   as nowSecond from dual; --结果:53


select to_date(&#39;2022-01-26 13:04:53&#39;,&#39;yyyy-mm-dd hh24:mi:ss&#39;)  from dual
Copy after login

3. Query the day of the week for a certain day

select to_char(to_date(&#39;2022-01-26&#39;,&#39;yyyy-mm-dd&#39;),&#39;day&#39;) from dual;   --结果:星期三
Copy after login

4. The direct difference in days between two dates

select floor(sysdate - to_date(&#39;20220101&#39;,&#39;yyyymmdd&#39;)) from dual;
Copy after login

5. Query an empty time type

select 1, TO_DATE(null) from dual;
Copy after login

6. Used to calculate the number of months between date1 and date2

select months_between(to_date(&#39;12-31-2021&#39;,&#39;MM-DD-YYYY&#39;),to_date(&#39;01-31-2021&#39;,&#39;MM-DD-YYYY&#39;)) "MONTHS" FROM DUAL; --结果:11
Copy after login

7. The date of the next day of the week (specified by char) at the specified time,

NEXT_DAY(date,char) 

select   next_day(sysdate,2) from dual;  --当前时间的下一个周一
--1表示星期日,2代表星期一
Copy after login

8. Get the number of days in this year

select add_months(trunc(sysdate,&#39;year&#39;), 12) - trunc(sysdate,&#39;year&#39;) from dual;
 --闰年的处理方法     
to_char( last_day( to_date(&#39;02&#39;|| :year,&#39;mmyyyy&#39;) ), &#39;dd&#39;)     
 --如果是28就不是闰年
Copy after login

9. Get the number of days in this year that the current time is

 select TO_CHAR(SYSDATE,&#39;DDD&#39;),sysdate from dual;
Copy after login

trunc[truncated to the nearest date, in days], the returned Date type

 select sysdate S1,                    
     trunc(sysdate) S2,                 //返回当前日期,无时分秒
     trunc(sysdate,&#39;year&#39;) YEAR,        //返回当前年的1月1日,无时分秒
     trunc(sysdate,&#39;month&#39;) MONTH ,     //返回当前月的1日,无时分秒
     trunc(sysdate,&#39;day&#39;) DAY           //返回当前星期的星期天,无时分秒
   from dual
Copy after login

10. Return the latest date in the date list

select greatest(&#39;2021-01-04&#39;,&#39;2022-01-04&#39;,&#39;2019-02-04&#39;) from dual;  --结果:2022-01-04
Copy after login

11. Calculate the time difference

 select floor(to_number(sysdate-to_date(&#39;2020-11-02 15:55:03&#39;,&#39;yyyy-mm-dd hh24:mi:ss&#39;))/365) as spanYears from dual        //时间差-年
select ceil(months_between(sysdate,to_date(&#39;2020-11-02 15:55:03&#39;,&#39;yyyy-mm-dd hh24:mi:ss&#39;))) as spanMonths from dual        //时间差-月
select floor(to_number(sysdate-to_date(&#39;2020-11-02 15:55:03&#39;,&#39;yyyy-mm-dd hh24:mi:ss&#39;))) as spanDays from dual             //时间差-天
select floor(to_number(sysdate-to_date(&#39;2020-11-02 15:55:03&#39;,&#39;yyyy-mm-dd hh24:mi:ss&#39;))*24) as spanHours from dual         //时间差-时
select floor(to_number(sysdate-to_date(&#39;2020-11-02 15:55:03&#39;,&#39;yyyy-mm-dd hh24:mi:ss&#39;))*24*60) as spanMinutes from dual    //时间差-分
select floor(to_number(sysdate-to_date(&#39;2020-11-02 15:55:03&#39;,&#39;yyyy-mm-dd hh24:mi:ss&#39;))*24*60*60) as spanSeconds from dual //时间差-秒
Copy after login

12. Find the first and last day of the month

 SELECT Trunc(Trunc(SYSDATE, &#39;MONTH&#39;) - 1, &#39;MONTH&#39;) First_Day_Last_Month,  --最后一月最后一天
       Trunc(SYSDATE, &#39;MONTH&#39;) - 1 / 86400 Last_Day_Last_Month,            --最后一月最后一天
       Trunc(SYSDATE, &#39;MONTH&#39;) First_Day_Cur_Month,                        --当前月第一天
       LAST_DAY(Trunc(SYSDATE, &#39;MONTH&#39;)) + 1 - 1 / 86400 Last_Day_Cur_Month --当前月最后一天
   FROM dual;
Copy after login

13. Query the time before the time (replace the minus sign with a plus sign after checking)

当前时间减去7分钟的时间
select sysdate,sysdate - interval &#39;7&#39; MINUTE from dual

当前时间减去7小时的时间
select sysdate - interval &#39;7&#39; hour from dual

当前时间减去7天的时间
select sysdate - interval &#39;7&#39; day from dual

当前时间减去7月的时间
select sysdate,sysdate - interval &#39;7&#39; month from dual

当前时间减去7年的时间
select sysdate,sysdate - interval &#39;7&#39; year from dual

时间间隔乘以一个数字(也就是8个小时*2倍,16个小时之前的数据)
select sysdate,sysdate - 8 *interval &#39;2&#39; hour from dual

 
获取七天之后的时间
select (sysdate + 7) from dual;

获取前一个月的时间(正数时是加月,负数时为减月)
select add_months(sysdate,-1) from dual;

select sysdate+1 from dual 加一天
select sysdate+1/24 from dual 加1小时
select sysdate+1/(24*60) from dual 加1分钟
select sysdate+1/(24*60*60) from dual 加1秒钟
Copy after login

Recommended tutorial: "Oracle Video Tutorial"

The above is the detailed content of Summary of some common situations of Oracle query based on time. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check tablespace size of oracle How to check tablespace size of oracle Apr 11, 2025 pm 08:15 PM

To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

How to encrypt oracle view How to encrypt oracle view Apr 11, 2025 pm 08:30 PM

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

How to view instance name of oracle How to view instance name of oracle Apr 11, 2025 pm 08:18 PM

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

How to uninstall Oracle installation failed How to uninstall Oracle installation failed Apr 11, 2025 pm 08:24 PM

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

How to import oracle database How to import oracle database Apr 11, 2025 pm 08:06 PM

Data import method: 1. Use the SQLLoader utility: prepare data files, create control files, and run SQLLoader; 2. Use the IMP/EXP tool: export data, import data. Tip: 1. Recommended SQL*Loader for big data sets; 2. The target table should exist and the column definition matches; 3. After importing, data integrity needs to be verified.

How to get time in oracle How to get time in oracle Apr 11, 2025 pm 08:09 PM

There are the following methods to get time in Oracle: CURRENT_TIMESTAMP: Returns the current system time, accurate to seconds. SYSTIMESTAMP: More accurate than CURRENT_TIMESTAMP, to nanoseconds. SYSDATE: Returns the current system date, excluding the time part. TO_CHAR(SYSDATE, 'YYY-MM-DD HH24:MI:SS'): Converts the current system date and time to a specific format. EXTRACT: Extracts a specific part from a time value, such as a year, month, or hour.

How to set up users of oracle How to set up users of oracle Apr 11, 2025 pm 08:21 PM

To create a user in Oracle, follow these steps: Create a new user using the CREATE USER statement. Grant the necessary permissions using the GRANT statement. Optional: Use the RESOURCE statement to set the quota. Configure other options such as default roles and temporary tablespaces.

How to create oracle dynamic sql How to create oracle dynamic sql Apr 12, 2025 am 06:06 AM

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values ​​to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

See all articles