当前位置:网站首页>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 !
边栏推荐
- Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!
- 差异与阵列和阵列结构和链表的区别
- Summary of basic debugging steps of S120 driver
- 3 -- Xintang nuc980 kernel supports JFFS2, JFFS2 file system production, kernel mount JFFS2, uboot network port settings, and uboot supports TFTP
- leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
- A new path for enterprise mid Platform Construction -- low code platform
- Google Earth Engine(GEE)——Landsat 全球土地调查 1975年数据集
- 压缩 js 代码就用 terser
- How to write test cases for test coupons?
- fasterxml ToStringSerializerBase报错
猜你喜欢
随机推荐
[software test] the most complete interview questions and answers. I'm familiar with the full text. If I don't win the offer, I'll lose
Remember the problem analysis of oom caused by a Jap query
What management points should be paid attention to when implementing MES management system
Django数据库(SQlite)基本入门使用教程
C#/VB.NET 删除Word文档中的水印
Wireshark installation
A new path for enterprise mid Platform Construction -- low code platform
KYSL 海康摄像头 8247 h9 isapi测试
实施MES管理系统时,哪些管理点是需要注意的
进程管理基础
How to build a 32core raspberry pie cluster from 0 to 1
Have fun | latest progress of "spacecraft program" activities
安德鲁斯—-多媒体编程
Code debugging core step memory
CDB PDB user rights management
Fundamentals of process management
Google Earth engine (GEE) -- 1975 dataset of Landsat global land survey
1个月增长900w+播放!总结B站顶流恰饭的2个新趋势
哈希表及完整注释
Number theory --- fast power, fast power inverse element


![[unity notes] screen coordinates to ugui coordinates](/img/e4/fc18dd9b4b0e36ec3e278e5fb3fd23.jpg)






