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.
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('2021-01-02 23:59:59', 'yyyy-mm-dd hh24:mi:ss');
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('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. Date and character conversion function usage (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. Query the day of the week for a certain day
select to_char(to_date('2022-01-26','yyyy-mm-dd'),'day') from dual; --结果:星期三
4. The direct difference in days between two dates
select floor(sysdate - to_date('20220101','yyyymmdd')) from dual;
5. Query an empty time type
select 1, TO_DATE(null) from dual;
6. Used to calculate the number of months between date1 and 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. 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代表星期一
8. Get the number of days in this year
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual; --闰年的处理方法 to_char( last_day( to_date('02'|| :year,'mmyyyy') ), 'dd') --如果是28就不是闰年
9. Get the number of days in this year that the current time is
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual;
trunc[truncated to the nearest date, in days], the returned Date type
select sysdate S1, trunc(sysdate) S2, //返回当前日期,无时分秒 trunc(sysdate,'year') YEAR, //返回当前年的1月1日,无时分秒 trunc(sysdate,'month') MONTH , //返回当前月的1日,无时分秒 trunc(sysdate,'day') DAY //返回当前星期的星期天,无时分秒 from dual
10. Return the latest date in the date list
select greatest('2021-01-04','2022-01-04','2019-02-04') from dual; --结果:2022-01-04
11. Calculate the time difference
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 //时间差-秒
12. Find the first and last day of the month
SELECT Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month, --最后一月最后一天 Trunc(SYSDATE, 'MONTH') - 1 / 86400 Last_Day_Last_Month, --最后一月最后一天 Trunc(SYSDATE, 'MONTH') First_Day_Cur_Month, --当前月第一天 LAST_DAY(Trunc(SYSDATE, 'MONTH')) + 1 - 1 / 86400 Last_Day_Cur_Month --当前月最后一天 FROM dual;
13. Query the time before the time (replace the minus sign with a plus sign after checking)
当前时间减去7分钟的时间 select sysdate,sysdate - interval '7' MINUTE from dual 当前时间减去7小时的时间 select sysdate - interval '7' hour from dual 当前时间减去7天的时间 select sysdate - interval '7' day from dual 当前时间减去7月的时间 select sysdate,sysdate - interval '7' month from dual 当前时间减去7年的时间 select sysdate,sysdate - interval '7' year from dual 时间间隔乘以一个数字(也就是8个小时*2倍,16个小时之前的数据) select sysdate,sysdate - 8 *interval '2' 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秒钟
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!

The development history of Oracle software from database to cloud computing includes: 1. Originated in 1977, it initially focused on relational database management system (RDBMS), and quickly became the first choice for enterprise-level applications; 2. Expand to middleware, development tools and ERP systems to form a complete set of enterprise solutions; 3. Oracle database supports SQL, providing high performance and scalability, suitable for small to large enterprise systems; 4. The rise of cloud computing services further expands Oracle's product line to meet all aspects of enterprise IT needs.

MySQL and Oracle selection should be based on cost, performance, complexity and functional requirements: 1. MySQL is suitable for projects with limited budgets, is simple to install, and is suitable for small to medium-sized applications. 2. Oracle is suitable for large enterprises and performs excellently in handling large-scale data and high concurrent requests, but is costly and complex in configuration.

Oracle helps businesses achieve digital transformation and data management through its products and services. 1) Oracle provides a comprehensive product portfolio, including database management systems, ERP and CRM systems, helping enterprises automate and optimize business processes. 2) Oracle's ERP systems such as E-BusinessSuite and FusionApplications realize end-to-end business process automation, improve efficiency and reduce costs, but have high implementation and maintenance costs. 3) OracleDatabase provides high concurrency and high availability data processing, but has high licensing costs. 4) Performance optimization and best practices include the rational use of indexing and partitioning technology, regular database maintenance and compliance with coding specifications.

Steps to delete the failed database after Oracle failed to build a library: Use sys username to connect to the target instance. Use DROP DATABASE to delete the database. Query v$database to confirm that the database has been deleted.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.