当前位置:网站首页>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 !
边栏推荐
- Leetcode:minimum_depth_of_binary_tree解决问题的方法
- 记一次JAP查询导致OOM的问题分析
- Django数据库(SQlite)基本入门使用教程
- Argo workflows source code analysis
- How to write test cases for test coupons?
- Error in fasterxml tostringserializerbase
- The panel floating with the mouse in unity can adapt to the size of text content
- Halcon instance to opencvsharp (C openCV) implementation -- bottle mouth defect detection (with source code)
- Unity webgl adaptive web page size
- 写作系列之contribution
猜你喜欢
Classify the features of pictures with full connection +softmax
fiddler的使用
数论 --- 快速幂、快速幂求逆元
Web3的先锋兵:虚拟人
The panel floating with the mouse in unity can adapt to the size of text content
postgresql之整體查詢大致過程
Google Earth Engine(GEE)——Landsat 全球土地调查 1975年数据集
What management points should be paid attention to when implementing MES management system
Dotconnect for DB2 Data Provider
[unity notes] screen coordinates to ugui coordinates
随机推荐
CDB PDB 用户权限管理
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?
Error in fasterxml tostringserializerbase
Code line breaking problem of untiy text box
一本揭秘字节万台节点ClickHouse背后技术实现的白皮书来了!
1--新唐nuc980 NUC980移植 UBOOT,从外部mx25l启动
Qpushbutton- "function refinement"
Niuke programming problem -- double pointer of 101 must be brushed
人脸识别应用解析
如何设计好接口测试用例?教你几个小技巧,轻松稿定
MySQL - common functions - string functions
Electrical engineering and automation
MMDetection3D加载毫米波雷达数据
How to design interface test cases? Teach you a few tips to draft easily
数论 --- 快速幂、快速幂求逆元
PCL 常用拟合模型及使用方法
Wireshark installation
Halcon instance to opencvsharp (C openCV) implementation -- bottle mouth defect detection (with source code)
安全巡检的工作
widerperson数据集转化为YOLO格式