search
HomeDatabaseMysql Tutorialoracle 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;

 

 

Statement
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
What are the differences in syntax between MySQL and other SQL dialects?What are the differences in syntax between MySQL and other SQL dialects?Apr 27, 2025 am 12:26 AM

MySQLdiffersfromotherSQLdialectsinsyntaxforLIMIT,auto-increment,stringcomparison,subqueries,andperformanceanalysis.1)MySQLusesLIMIT,whileSQLServerusesTOPandOracleusesROWNUM.2)MySQL'sAUTO_INCREMENTcontrastswithPostgreSQL'sSERIALandOracle'ssequenceandt

What is MySQL partitioning?What is MySQL partitioning?Apr 27, 2025 am 12:23 AM

MySQL partitioning improves performance and simplifies maintenance. 1) Divide large tables into small pieces by specific criteria (such as date ranges), 2) physically divide data into independent files, 3) MySQL can focus on related partitions when querying, 4) Query optimizer can skip unrelated partitions, 5) Choosing the right partition strategy and maintaining it regularly is key.

How do you grant and revoke privileges in MySQL?How do you grant and revoke privileges in MySQL?Apr 27, 2025 am 12:21 AM

How to grant and revoke permissions in MySQL? 1. Use the GRANT statement to grant permissions, such as GRANTALLPRIVILEGESONdatabase_name.TO'username'@'host'; 2. Use the REVOKE statement to revoke permissions, such as REVOKEALLPRIVILEGESONdatabase_name.FROM'username'@'host' to ensure timely communication of permission changes.

Explain the differences between InnoDB and MyISAM storage engines.Explain the differences between InnoDB and MyISAM storage engines.Apr 27, 2025 am 12:20 AM

InnoDB is suitable for applications that require transaction support and high concurrency, while MyISAM is suitable for applications that require more reads and less writes. 1.InnoDB supports transaction and bank-level locks, suitable for e-commerce and banking systems. 2.MyISAM provides fast read and indexing, suitable for blogging and content management systems.

What are the different types of JOINs in MySQL?What are the different types of JOINs in MySQL?Apr 27, 2025 am 12:13 AM

There are four main JOIN types in MySQL: INNERJOIN, LEFTJOIN, RIGHTJOIN and FULLOUTERJOIN. 1.INNERJOIN returns all rows in the two tables that meet the JOIN conditions. 2.LEFTJOIN returns all rows in the left table, even if there are no matching rows in the right table. 3. RIGHTJOIN is contrary to LEFTJOIN and returns all rows in the right table. 4.FULLOUTERJOIN returns all rows in the two tables that meet or do not meet JOIN conditions.

What are the different storage engines available in MySQL?What are the different storage engines available in MySQL?Apr 26, 2025 am 12:27 AM

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

What are some common security vulnerabilities in MySQL?What are some common security vulnerabilities in MySQL?Apr 26, 2025 am 12:27 AM

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

How can you identify slow queries in MySQL?How can you identify slow queries in MySQL?Apr 26, 2025 am 12:15 AM

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

See all articles

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use