当前位置:网站首页>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 !
边栏推荐
- Application analysis of face recognition
- MMDetection3D加载毫米波雷达数据
- MySQL - common functions - string functions
- Django数据库(SQlite)基本入门使用教程
- leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
- 你不可不知道的Selenium 8种元素定位方法,简单且实用
- 导数、偏导数、方向导数
- Derivative, partial derivative, directional derivative
- LeetCode 77:组合
- NuScenes数据集关于Radar数据的统计
猜你喜欢
Web3's need for law
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
S120驱动器基本调试步骤总结
Go swagger use
Classify the features of pictures with full connection +softmax
6-6漏洞利用-SSH安全防御
The 8 element positioning methods of selenium that you have to know are simple and practical
Overall query process of PostgreSQL
KYSL 海康摄像头 8247 h9 isapi测试
Google Earth Engine(GEE)——Landsat 全球土地调查 1975年数据集
随机推荐
This week's hot open source project!
Leetcode:minimum_ depth_ of_ binary_ Tree solutions
Convert widerperson dataset to Yolo format
HAVE FUN | “飞船计划”活动最新进展
How to build a 32core raspberry pie cluster from 0 to 1
Remember the problem analysis of oom caused by a Jap query
Kysl Haikang camera 8247 H9 ISAPI test
Error in fasterxml tostringserializerbase
【Node学习笔记】chokidar模块实现文件监听
Leetcode:minimum_depth_of_binary_tree解决问题的方法
Application analysis of face recognition
【森城市】GIS数据漫谈(二)
6-6漏洞利用-SSH安全防御
Difference and the difference between array and array structure and linked list
Planning and design of double click hot standby layer 2 network based on ENSP firewall
基于ensp防火墙双击热备二层网络规划与设计
Apifox,你的API接口文档卷成这样了吗?
安全交付工程师
unity 自定义webgl打包模板
MySQL