当前位置:网站首页>Examples of how to use dates in Oracle
Examples of how to use dates in Oracle
2022-07-07 02:45:00 【1024 questions】
Preface
1 The date is equal to \ Greater than \ Less than
2 Date difference calculation
1 Directly subtract the two times
2 Two level conversion of time and date
3 Use trunc function
summary
Preface In the use of sql Function time , Due to different project databases , Lead to sql The use of dates in is very different , Some common date calculations , There are different applications , So sort out the records Oracle Date usage of .
With user Table as an example
CREATE TABLE "SCOTT"."user" ("id" NUMBER(20,0) NOT NULL ENABLE, "name" VARCHAR2(30), "age" NUMBER(11,0), "email" VARCHAR2(50), "crt_time" DATE, PRIMARY KEY ("id") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 NOCOMPRESS LOGGING TABLESPACE "USERS" ENABLE ) SEGMENT CREATION DEFERRED PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING TABLESPACE "USERS"-- Add basic data INSERT INTO "SCOTT"."user"("id", "name", "age", "email", "crt_time") VALUES ('1', 'Jone', '18', '''[email protected]', TO_DATE('2022-04-01 22:20:02', 'SYYYY-MM-DD HH24:MI:SS'));INSERT INTO "SCOTT"."user"("id", "name", "age", "email", "crt_time") VALUES ('2', 'Jone', '20', '''[email protected]', TO_DATE('2022-04-02 22:20:02', 'SYYYY-MM-DD HH24:MI:SS'));INSERT INTO "SCOTT"."user"("id", "name", "age", "email", "crt_time") VALUES ('3', 'Jone', '28', '''[email protected]', TO_DATE('2022-04-03 22:20:02', 'SYYYY-MM-DD HH24:MI:SS'));INSERT INTO "SCOTT"."user"("id", "name", "age", "email", "crt_time") VALUES ('4', 'Jone', '21', '''[email protected]', TO_DATE('2022-04-04 22:20:02', 'SYYYY-MM-DD HH24:MI:SS'));INSERT INTO "SCOTT"."user"("id", "name", "age", "email", "crt_time") VALUES ('5', 'Jone', '24', '''[email protected]', TO_DATE('2022-04-05 22:20:02', 'SYYYY-MM-DD HH24:MI:SS'));INSERT INTO "SCOTT"."user"("id", "name", "age", "email", "crt_time") VALUES ('6', 'Jone', '12', '''[email protected]', TO_DATE('2022-04-06 22:20:02', 'SYYYY-MM-DD HH24:MI:SS'));1 The date is equal to \ Greater than \ Less than Oracle The size of the date in , It is usually converted into a string for comparison .
explain :
dual yes oracle An internal table defined in , Only one row and one column
SYSDATE The function represents the current time
to_char(date,‘format’) function , Parameters date For date type , Parameters format Represents the format of the conversion , What is commonly used is 'yyyy-mm-dd hh24:mi:ss' ( Case sensitive )
-- Look at two times select TO_DATE('2022-04-04 22:20:02', 'SYYYY-MM-DD HH24:MI:SS') , SYSDATE FROM dual-- result -- 2022-04-04 22:20:022022-04-10 22:31:43-- A simple case -- 1 Time comparison Because the incoming time is less than the current time Result set is empty select TO_DATE('2022-04-04 22:20:02', 'SYYYY-MM-DD HH24:MI:SS') , SYSDATE FROM dualwhere TO_DATE('2022-04-04 22:20:02', 'SYYYY-MM-DD HH24:MI:SS') > SYSDATE -- Empty string Empty string -- 2 Time comparison Because the incoming time is less than the current time Result set has value select TO_DATE('2022-04-04 22:20:02', 'SYYYY-MM-DD HH24:MI:SS') , SYSDATE FROM dualwhere TO_DATE('2022-04-04 22:20:02', 'SYYYY-MM-DD HH24:MI:SS') < SYSDATE -- 2022-04-04 22:20:022022-04-10 22:37:502 Date difference calculation demand : Calculate the difference in days between the two times
1 Directly subtract the two timesSubtracting two dates , What you get is data with an accuracy of minutes and seconds , Unqualified
SELECT SYSDATE- to_date('2022-04-10 22:20:02','yyyy-mm-dd hh24:mi:ss')FROM dual-- result 0.01813657407407407407407407407407407407412 Two level conversion of time and date to_date(string,‘format’) function , Is to convert a string to date Time .
SELECT to_date(to_char(SYSDATE,'yyyy-mm-dd'),'YYYY-MM-DD HH24:MI:SS') - to_date(to_char(to_date( '2022-04-10 22:20:02', 'YYYY-MM-DD HH24:MI:SS' ) ,'yyyy-mm-dd'),'YYYY-MM-DD HH24:MI:SS')FROM dual;-- result : 0 This method is cumbersome , And after many transformations , Low efficiency .
3 Use trunc functiontrunc() function .
effect : Used to intercept time or value , Returns the specified value .
Date processing :
TRUNC(date,[format])
date For the necessary parameters , Is an input date Date value .
format Parameters can be ignored , Specify date format , By default, it indicates the of the specified date 0 spot .
SELECTSYSDATE,to_date( '2022-04-10 22:20:02', 'YYYY-MM-DD HH24:MI:SS' ), trunc(SYSDATE) - trunc(to_date( '2022-04-10 22:20:02', 'YYYY-MM-DD HH24:MI:SS' ))FROM dual;Besides , This function can also be used for numerical processing .
TRUNC(number,[decimals])
number For the necessary parameters , Is an input number The number .
decimals Parameters can be ignored , Specify the number of bits to intercept , By default, the value after the decimal point is truncated
SELECT3.0,2.22, trunc(3.0) - trunc(2.22,1)FROM dual;-- result 3 2.22 0.8 summary This is about Oracle That's all for the articles used in the middle date , More about Oracle Please search the previous articles of SDN or continue to browse the relevant articles below. I hope you will support SDN more in the future !
边栏推荐
- Digital scrolling increases effect
- Compress JS code with terser
- Google Earth engine (GEE) -- 1975 dataset of Landsat global land survey
- Wireshark installation
- LeetCode 77:组合
- Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
- Application analysis of face recognition
- Oracle中日期的使用方法实例
- Linear list --- circular linked list
- Remember the problem analysis of oom caused by a Jap query
猜你喜欢

Cloud Mail .NET Edition
Django数据库(SQlite)基本入门使用教程

AWS学习笔记(一)

Five reasons for clothing enterprises to deploy MES management system

C#/VB. Net to delete watermarks in word documents

MySQL --- 常用函数 - 字符串函数

C语言练习题_1

Argo workflows source code analysis
![leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]](/img/0d/e07fe970167368040eb09b05c3682e.png)
leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]

运维管理系统有哪些特色
随机推荐
3 -- Xintang nuc980 kernel supports JFFS2, JFFS2 file system production, kernel mount JFFS2, uboot network port settings, and uboot supports TFTP
unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
[node learning notes] the chokidar module realizes file monitoring
数论 --- 快速幂、快速幂求逆元
Niuke programming problem -- double pointer of 101 must be brushed
postgresql之整體查詢大致過程
MMDetection3D加载毫米波雷达数据
Leetcode:minimum_ depth_ of_ binary_ Tree solutions
【2022国赛模拟】多边形——计算几何、二分答案、倍增
The 8 element positioning methods of selenium that you have to know are simple and practical
如何设计好接口测试用例?教你几个小技巧,轻松稿定
Argo workflows source code analysis
CDB PDB 用户权限管理
Halcon instance to opencvsharp (C openCV) implementation -- bottle mouth defect detection (with source code)
Real project, realized by wechat applet opening code (end)
1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
Apifox, is your API interface document rolled up like this?
QT common Concepts-1
MySQL
MySQL提升大量数据查询效率的优化神器