Summary of some common situations of Oracle query based on time
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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_

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.

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.

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.

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.

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.

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.

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.
