Home Database Mysql Tutorial oracle date 操作

oracle date 操作

Jun 07, 2016 pm 03:38 PM
date oracle use operate

使用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;

 

 

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to paginate oracle database How to paginate oracle database Apr 11, 2025 pm 08:42 PM

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

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.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

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

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

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.

How to read the oracle awr report How to read the oracle awr report Apr 11, 2025 pm 09:45 PM

An AWR report is a report that displays database performance and activity snapshots. The interpretation steps include: identifying the date and time of the activity snapshot. View an overview of activities and resource consumption. Analyze session activities to find session types, resource consumption, and waiting events. Find potential performance bottlenecks such as slow SQL statements, resource contention, and I/O issues. View waiting events, identify and resolve them for performance. Analyze latch and memory usage patterns to identify memory issues that are causing performance issues.

See all articles