当前位置:网站首页>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:50
2 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.0181365740740740740740740740740740740741
2 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 !
边栏推荐
- How to design interface test cases? Teach you a few tips to draft easily
- CSDN 夏令营课程 项目分析
- Halcon instance to opencvsharp (C openCV) implementation -- bottle mouth defect detection (with source code)
- Electrical engineering and automation
- Pioneer of Web3: virtual human
- 一本揭秘字节万台节点ClickHouse背后技术实现的白皮书来了!
- KYSL 海康摄像头 8247 h9 isapi测试
- Code debugging core step memory
- QT common Concepts-1
- [leetcode]Search for a Range
猜你喜欢
C语言练习题_1
Ali yunyili: how does yunyuansheng solve the problem of reducing costs and improving efficiency?
Hash table and full comments
3--新唐nuc980 kernel支持jffs2, Jffs2文件系统制作, 内核挂载jffs2, uboot网口设置,uboot支持tftp
Classify the features of pictures with full connection +softmax
How to build a 32core raspberry pie cluster from 0 to 1
Douban average 9 x. Five God books in the distributed field!
慧通编程入门课程 - 2A闯关
Kysl Haikang camera 8247 H9 ISAPI test
测试优惠券要怎么写测试用例?
随机推荐
Cloud Mail . NET Edition
unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
What management points should be paid attention to when implementing MES management system
QT常见概念-1
安全交付工程师
Matlb| economic scheduling with energy storage, opportunity constraints and robust optimization
Error in fasterxml tostringserializerbase
压缩 js 代码就用 terser
运维管理系统有哪些特色
[unity notes] screen coordinates to ugui coordinates
HAVE FUN | “飞船计划”活动最新进展
从零安装Redis
Kysl Haikang camera 8247 H9 ISAPI test
QPushButton-》函数精解
3--新唐nuc980 kernel支持jffs2, Jffs2文件系统制作, 内核挂载jffs2, uboot网口设置,uboot支持tftp
Classify the features of pictures with full connection +softmax
[Mori city] random talk on GIS data (II)
Mmdetection3d loads millimeter wave radar data
Rethinking of investment
Compress JS code with terser