oracle date 操作
使用oracle的date类型时,最好使用24小时制。date类型精确的秒。 oracle的日期在24小时制中00:00:00 在Oracle中表示为叫做上午12点,也就是零点。我们中午12点叫做下午12点。呵呵。 世界是这样循环的 : 上午12点 -上午12点01 -上午1点 --上午11:59 :59
使用oracle的date类型时,最好使用24小时制。date类型精确的秒。
oracle的日期在24小时制中00:00:00 在Oracle中表示为叫做上午12点,也就是零点。我们中午12点叫做下午12点。呵呵。
世界是这样循环的 : 上午12点 -上午12点01 -上午1点 --上午11:59 :59 --下午12点 - 下午12点 02 - 下午1点 -下午11:59:59
关于日期函数 add_months (d,n) 和last_day(d),round(d,【fmt】)
add_months 中的d 如果没有时间值,结果为下个月的当前日的开始日期 -00:00:00上午12点。 如果有时间值 ,结果为下月的当前日的时间加时间值。
如果d为当前月的最后一天,结果为下个月的最后一天。哈哈。
例如:add_months(to_date('2007-02-27 12:01','yyyy-mm-dd hh24:mi'),1) = 2007-3-27 12:01:00
add_months(to_date('2007-02-27','yyyy-mm-dd '),1) = 2007-03-27 00:00:00
add_months(to_date('2007-02-28 12:01','yyyy-mm-dd hh24:mi'),1) = 2007-3-31 12:01:00
last_day 当前月的最后一天的00:00。
round 四舍五入 ,格式为year ,舍入到1月1日, month ,舍入到该月第一日, 大于16日,为下月。day 最近的星期天。
例如:
round( (to_date('2007-10-19 13:00:00 ','yyyy-mm-dd hh24:mi:ss') ),'day' ) = 2007-10-21
trunc 与round相似,但不四舍五入。
trunc(d,fmt)
extract (fmt from d)
fmt 可取为 year, month, day , hour, minute ,second .
总结:oracle的日期函数默认操作日期,既不包括时间。 如果有时间的话就加上时间,但不做处理。默认都是day。
Oracle关于时间/日期的操作
1.日期时间间隔操作
当前时间减去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
时间间隔乘以一个数字
select sysdate,sysdate - 8 *interval ’2’ hour from dual
2.日期到字符操作
select sysdate,to_char(sysdate,’yyyy-mm-dd hh24:mi:ss’) from dual
select sysdate,to_char(sysdate,’yyyy-mm-dd hh:mi:ss’) from dual
select sysdate,to_char(sysdate,’yyyy-ddd hh:mi:ss’) from dual
select sysdate,to_char(sysdate,’yyyy-mm iw-d hh:mi:ss’) from dual
参考oracle的相关关文档(ORACLE901DOC/SERVER.901/A90125/SQL_ELEMENTS4.HTM#48515)
3. 字符到日期操作
select to_date(’2003-10-17 21:15:37’,’yyyy-mm-dd hh24:mi:ss’) from dual
具体用法和上面的to_char差不多。
4. trunk/ ROUND函数的使用
select trunc(sysdate ,’YEAR’) from dual
select trunc(sysdate ) from dual
select to_char(trunc(sysdate ,’YYYY’),’YYYY’) from dual
5.oracle有毫秒级的数据类型
--返回当前时间 年月日小时分秒毫秒
select to_char(current_timestamp(5),’DD-MON-YYYY HH24:MI:SSxFF’) from dual;
--返回当前 时间的秒毫秒,可以指定秒后面的精度(最大=9)
select to_char(current_timestamp(9),’MI:SSxFF’) from dual;
6.计算程序运行的时间(ms)
declare
type rc is ref cursor;
l_rc rc;
l_dummy all_objects.object_name%type;
l_start number default dbms_utility.get_time;
begin
for I in 1 .. 1000
loop
open l_rc for
’select object_name from all_objects ’||
’where object_id = ’ || i;
fetch l_rc into l_dummy;
close l_rc;
end loop;
dbms_output.put_line
( round( (dbms_utility.get_time-l_start)/100, 2 ) ||
’ seconds...’ );
end;
№1:取得当前日期是本月的第几周
SQL> select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual;
TO_CHAR(SYSDATE,'YY
-------------------
20030327 4 18:16:09
SQL> select to_char(sysdate,'W') from dual;
T
-
4
№2:取得当前日期是一个星期中的第几天,注意星期日是第一天
SQL> select sysdate,to_char(sysdate,'D') from dual;
SYSDATE T
--------- -
27-MAR-03 5
类似:
select to_char(sysdate,'yyyy') from dual; --年
select to_char(sysdate,'Q' from dual; --季
select to_char(sysdate,'mm') from dual; --月
select to_char(sysdate,'dd') from dual; --日
ddd 年中的第几天
WW 年中的第几个星期
W 该月中第几个星期
D 周中的星期几
hh 小时(12)
hh24 小时(24)
Mi 分
ss 秒
№3:取当前日期是星期几中文显示:
SQL> select to_char(sysdate,'day') from dual;
TO_CHAR(SYSDATE,'DAY')
----------------------
星期四
№4:如果一个表在一个date类型的字段上面建立了索引,如何使用
alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
№5: 得到当前的日期
select sysdate from dual;
№6: 得到当天凌晨0点0分0秒的日期
select trunc(sysdate) from dual;
-- 得到这天的最后一秒
select trunc(sysdate) + 0.99999 from dual;
-- 得到小时的具体数值
select trunc(sysdate) + 1/24 from dual;
select trunc(sysdate) + 7/24 from dual;
№7:得到明天凌晨0点0分0秒的日期
select trunc(sysdate+1) from dual;
select trunc(sysdate)+1 from dual;
№8: 本月一日的日期
select trunc(sysdate,'mm') from dual;
№9:得到下月一日的日期
select trunc(add_months(sysdate,1),'mm') from dual;
№10:返回当前月的最后一天?
select last_day(sysdate) from dual;
select last_day(trunc(sysdate)) from dual;
select trunc(last_day(sysdate)) from dual;
select trunc(add_months(sysdate,1),'mm') - 1 from dual;
№11: 得到一年的每一天
select trunc(sysdate,'yyyy')+ rn -1 date0
from
(select rownum rn from all_objects
where rownum
№12:今天是今年的第N天
SELECT TO_CHAR(SYSDATE,'DDD') FROM DUAL;
№13:如何在给现有的日期加上2年
select add_months(sysdate,24) from dual;
№14:判断某一日子所在年分是否为润年
select decode(to_char(last_day(trunc(sysdate,'y')+31),'dd'),'29','闰年','平年') from dual;
№15:判断两年后是否为润年
select decode(to_char(last_day(trunc(add_months(sysdate,24),'y')+31),'dd'),'29','闰年','平年') from dual;
№16:得到日期的季度
select ceil(to_number(to_char(sysdate,'mm'))/3) from dual;
select to_char(sysdate, 'Q') from dual;

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

The function in Oracle to calculate the number of days between two dates is DATEDIFF(). The specific usage is as follows: Specify the time interval unit: interval (such as day, month, year) Specify two date values: date1 and date2DATEDIFF(interval, date1, date2) Return the difference in days

The retention period of Oracle database logs depends on the log type and configuration, including: Redo logs: determined by the maximum size configured with the "LOG_ARCHIVE_DEST" parameter. Archived redo logs: Determined by the maximum size configured by the "DB_RECOVERY_FILE_DEST_SIZE" parameter. Online redo logs: not archived, lost when the database is restarted, and the retention period is consistent with the instance running time. Audit log: Configured by the "AUDIT_TRAIL" parameter, retained for 30 days by default.

The Oracle database startup sequence is: 1. Check the preconditions; 2. Start the listener; 3. Start the database instance; 4. Wait for the database to open; 5. Connect to the database; 6. Verify the database status; 7. Enable the service (if necessary ); 8. Test the connection.

The INTERVAL data type in Oracle is used to represent time intervals. The syntax is INTERVAL <precision> <unit>. You can use addition, subtraction, multiplication and division operations to operate INTERVAL, which is suitable for scenarios such as storing time data and calculating date differences.

To find the number of occurrences of a character in Oracle, perform the following steps: Get the total length of a string; Get the length of the substring in which a character occurs; Count the number of occurrences of a character by subtracting the substring length from the total length.

Oracle database server hardware configuration requirements: Processor: multi-core, with a main frequency of at least 2.5 GHz. For large databases, 32 cores or more are recommended. Memory: At least 8GB for small databases, 16-64GB for medium sizes, up to 512GB or more for large databases or heavy workloads. Storage: SSD or NVMe disks, RAID arrays for redundancy and performance. Network: High-speed network (10GbE or higher), dedicated network card, low-latency network. Others: Stable power supply, redundant components, compatible operating system and software, heat dissipation and cooling system.

The amount of memory required by Oracle depends on database size, activity level, and required performance level: for storing data buffers, index buffers, executing SQL statements, and managing the data dictionary cache. The exact amount is affected by database size, activity level, and required performance level. Best practices include setting the appropriate SGA size, sizing SGA components, using AMM, and monitoring memory usage.

The method of replacing strings in Oracle is to use the REPLACE function. The syntax of this function is: REPLACE(string, search_string, replace_string). Usage steps: 1. Identify the substring to be replaced; 2. Determine the new string to replace the substring; 3. Use the REPLACE function to replace. Advanced usage includes: multiple replacements, case sensitivity, special character replacement, etc.
