当前位置:网站首页>Oracle: time type
Oracle: time type
2022-06-09 07:26:00 【Cat fearless mouse a】
Oracle The type of time in
1、Oracle The only time type in is date and TIMESTAMP,TIMESTAMP It's better than date More precise types
⑴DATE type :ORACLE The most common date type , It can save dates and times , Common date processing can use this type
⑵TIMESTAMP type :ORACLE Common date types , It is associated with date Is the difference between the TIMESTAMP Type can not only save date and time , It can also save fractional seconds , The number of decimal places can be specified as 0-9, The default is 6 position
2、 The date time function is used to process time type data ,Oracle Many date related functions are provided in . Plus or minus dates , There are some rules for adding and subtracting dates
⑴ date - Numbers = date
⑵ date + Numbers = date ( God )
⑶ date - date = Numbers ( God )
⑷ stay Oracle in ,date Type can add or subtract days directly , And the months of addition and subtraction need to be used add_months function
⑸TIMESTAMP If the date type is added or subtracted from the value, it will be automatically converted to DATE type , In other words, fractional seconds will be automatically removed
3、 stay Oracle To be exact, a week begins on Sunday and ends on Saturday , The time difference is in days
Oracle Medium date type
date type : It means year, month, day, hour, minute, second
⑴ The display format can be :YYYY/MM/DD, It can also be for YYYY/MM/DD HH24:MI:SS
⑵ There is only one storage format :YYYY/MM/DD HH24:MI:SS
⑶ namely ,date The value of type can have different display formats in the database graphical interface ( The specific display format is determined by the tool settings ), But the actual storage format is only one
⑷ When you only save the year, month and day ,date What's actually stored is : Specific date 0 when 0 branch 0 second , stay plsql in , The display format is :YYYY/MM/DD
example 1:

Oracle Medium timestamp type
1、date and timestamp They are all expressions of date and time , Only the accuracy of the two types is different , The former is accurate to seconds , The latter is accurate to decimal seconds
2、timestamp Short for timestamp . Time stamps can store centuries 、4 Year of the year 、 month 、 Japan 、 when ( With 24 Hour format )、 branch 、 second . And DATE Type comparison , Timestamps have the following advantages :
⑴ Timestamps can store decimal places of seconds
⑵ Time stamps can store time zones
3、TIMESTAMP data type , It includes all DATE Information of data type: year, day, hour, minute and second , It also includes information about decimals and seconds . If you want to put the DATE Type conversion to TIMESTAMP type , Just use CAST function
Oracle Date format in
| Format | describe |
| YYYY | Four digit year |
| YYY,YY,Y | The last three digits of the year 、 Two or one , The default is the current century |
| MM | 01~12 Month number of |
| MONTH | Nine character month , Fill in the space on the right |
| MON | Three character month abbreviation |
| WW | The week of the year |
| D | Day of the week |
| DD | The day of the month |
| DDD | The day of the year |
| DAY | The full name of a nine character day , Fill in the blanks on the right |
| HH,HH12 | Hours of the day ,12 Decimal notation |
| HH24 | Hours of the day , The value is 00~23 |
| MI | Minutes in an hour |
| SS | Seconds in a minute |
| SSSS | The number of seconds since midnight |
Oracle Time function
sysdate
effect :sysdate The time() function returns the time of the current system ( return date data type )
example 2: Inquire about 

example 2_1: Insert
sql:insert into USER_INFO (ID,NAME,COUNTRY,DATE_TIME,TIMESTAMP) values(3,' Wang Wu ','China',sysdate,sysdate)
systimestamp
effect : stay Oracle in ,systimestamp Function returns the current system date and time on the local database ( Including microseconds and time zones )
example 3: Inquire about 
example 3_1: Insert
insert into USER_INFO (ID,NAME,COUNTRY,DATE_TIME,TIMESTAMP) values(4,' Li Liu ','China',systimestamp,systimestamp)
notes : It can be seen that
sysdate The function is best used for date Data of type ,systimestamp Best used for timestamp Data of type . Of course, the two can be mixed , Just a little difference
to_date()
effect : Convert character type into date type in a certain format
example 4: Query data 
example 4_1: insert data
INSERT INTO USER_INFO (ID,NAME,CREATE_TIME,UPDATE_TIME) VALUES (9,' Small m',to_date('2020-07-29 18:00:00','yyyy-mm-dd hh24:mi:ss'),to_date('2020-07-29 18:00:00','yyyy-mm-dd hh24:mi:ss'))
notes :
1、to_date() Only time type strings without millisecond level can be formatted
⑴to_date() Is used to convert string time to date type , The date type includes year, day, hour, minute and second
2、 If the field is a timetsmp Type of , Then the default millisecond level will be 0
3、Oracle Query data according to time Mysql There's a big difference
⑴ stay Mysql You can query directly according to the time string in :SELECT * FROM roleinfo WHERE logintime = "2019-01-25";
⑵ stay Oracle When querying by time in , You must first convert the time string to date type
4、 Hour format : No 24 when , The default is 12 hourly ,24 The hour format conversion needs to specify
example 4_2:
to_timestamp()
effect : Convert character types into date timestamp types in a certain format (timestamp type )
example 4_3: Query data


example 4_4:
insert into user_info (ID,
NAME,
COUNTRY,
DATE_TIME,
TIMESTAMP,
DEPT_ID,
EMPLOYEE_NO,
LEADER_NO) values (6,'LL','yy',to_timestamp('2020-07-30 20:30:30.123400','yyyy-mm-dd hh24:mi:ss.ff'),to_timestamp('2020-07-30 20:30:30.123','yyyy-mm-dd hh24:mi:ss.ff3'),1,1,1)
to_char()
1、 effect : Change the date to a character type in a certain format
2、 Formatting timestamp Time value of type , You can format milliseconds as needed
⑴ff:6 Bit millisecond value
⑵ff3:3 Bit millisecond value
⑶ It can also be done without a millisecond value
3、 If you are formatting a date Time value of type , So you can't add a millisecond parameter to the format string ,date There are no milliseconds in the type
4、date Type can only be accurate to seconds , To be precise in milliseconds , Need to use timestamp type
example 5: Query data
select to_char(CREATE_TIME,'yyyy-mm-dd hh24:mi:ss'),to_char(UPDATE_TIME,'yyyy-mm-dd hh24:mi:ss.ff') from USER_INFO where CREATE_TIME > to_date('2020-07-29 19:00:00','yyyy-mm-dd hh24:mi:ss');
example 5_1: Without milliseconds 
example 5_2: belt 3 Bit milliseconds 
example 5_3: other
select to_char(sysdate,'yyyy') as nowYear from dual; -- The year to get time
select to_char(sysdate,'mm') as nowMonth from dual; -- Get the month of time
select to_char(sysdate,'dd') as nowDay from dual; -- Get the day of time
select to_char(sysdate,'hh24') as nowHour from dual; -- The time to get time
select to_char(sysdate,'mi') as nowMinute from dual; -- Get the minutes of time
select to_char(sysdate,'ss') as nowSecond from dual; -- Get the seconds of time example 5_4: Query data



notes :
although sysdate、systimestamp、to_date()、to_timestamp() Can be used to generate a date Type or timestamp type , But there will be some default gaps . So don't mix them up , As for the type of the field itself, use the function of the corresponding type to create a time value
⑴ Generate a date type : Use sysdate or to_date()
⑵ Generate a timestamp type : Use systimestamp or to_timestamp()
⑶ Will a date Type or timestamp The time value of type is converted to string type : Use to_char(), It just means that you need to distinguish when specifying the format
Oracle Date addition and subtraction
Oracle There are many ways to add or subtract dates in , Here are three
⑴ One is the operation for days : It is applicable to Japan , when , branch , Second operation
⑵ One is the operation of the month : For month , Years of operation
⑶ One is to use INTERVAL function : Applicable to , month , Japan , when , branch , Second operation
Add and subtract numbers directly
1、 The addition and subtraction of days are mainly in sysdate Go ahead directly on the day , when , branch , The addition and subtraction of seconds
2、Oracle Use in sysdate Function to get the current system time
| Method | describe |
| SYSDATE | current time |
| SYSDATE+1 | Add a day |
| SYSDATE+(1/24) | Add an hour |
| SYSDATE+(1/24/60) | One more minute |
| SYSDATE+(1/24/60/60) | Add one second |
| SYSDATE-1 | One day less |
example 6: Query data 
example 6_1: insert data 

adopt ADD_MONTHS() function
| Method | describe |
| ADD_MONTHS(SYSDATE, 1) | Add a month |
| ADD_MONTHS(SYSDATE, -1) | Minus one month |
| ADD_MONTHS(SYSDATE, 1*12) | Add a year |
| ADD_MONTHS(SYSDATE, -1*12) | Minus one year |
Use INTERVAL function
| Method | describe |
| SYSDATE + INTERVAL '1' YEAR | Add a year |
| SYSDATE + INTERVAL '-1' YEAR | Minus one year |
| SYSDATE + INTERVAL '1' MONTH | Add a month |
| SYSDATE + INTERVAL '1' DAY | Add a day |
| SYSDATE + INTERVAL '1' HOUR | Add an hour |
| SYSDATE + INTERVAL '1' MINUTE | One more minute |
| SYSDATE + INTERVAL '1' SECOND | Add a second |
trunc() function
explain :Oracle Medium trunc() Function is used to intercept time type or number
grammar :TRUNC(number,num_digits)
Parameters :
⑴Number: A number or time that requires truncation
⑵Num_digits: Number used to specify rounding precision .Num_digits The default value of 0
notes :
1、TRUNC() Function does not round when intercepting
2、to_char Function support date and timestamp, however trunc But I do not support TIMESTAMP data type
Processing date
TRUNC() The date processing method of the function is : The intercepted part retains its value , The UN intercepted part is saved as 0
| Method | describe |
| trunc(sysdate,'dd') | Return to the current date ( Accurate to the sky ) |
| trunc(sysdate,'hh') | Returns the year, month, day and hour of the current time ( Accurate to the hour ) |
| trunc(sysdate,'mi') | Returns the time of the current time ( To the minute , There is no such thing as a second ) |
| trunc(sysdate,'mm') | Back to the first day of the month |
| trunc(sysdate,'yy') | Back to the first day of the year |
| trunc(sysdate,'yyyy') | Back to the first day of the year |
| trunc(sysdate,'d') | Return to the first day of the current week ( Sunday ) |
example 7:dd
INSERT INTO USER_INFO(ID,NAME,CREATE_TIME,UPDATE_TIME) VALUES(5,' Xiaohong ',trunc(sysdate,'dd'),trunc(sysdate,'dd'));
-- Accurate to the sky , Then the time and second values are 0
example 7_1:
INSERT INTO USER_INFO(ID,NAME,CREATE_TIME,UPDATE_TIME) VALUES(6,' Xiao Ming ',trunc(sysdate,'hh'),trunc(sysdate,'hh'));
-- Accurate to the hour , Then the value of minutes and seconds is 0
example 7_2:sysdate You can also add or subtract time
-- After the intercept time , Add and subtract time
INSERT INTO USER_INFO(ID,NAME,CREATE_TIME,UPDATE_TIME) VALUES(7,' Xiao Zhang ',(trunc(sysdate,'hh')+2/24),(trunc(sysdate,'hh')+30/24/60));
Processing values
1、TRUNC(n1, n2): Used to base on n2 Intercept n1 And return the result ,n2 Omission . Truncated values are not rounded
2、n2 Parameters can be positive or negative
⑴ A negative number means that it is truncated from the first digit to the left of the decimal point and then set to zero
⑵ A positive number means to keep one digit after the decimal point
⑶ Truncate the integral part : Can not pass n2 Parameters can also be passed N2=0
example 8:
example 8_1:
expand
timestamp keyword
Oracle Can also be used in timestamp Keyword to convert a time string value to date type
example 9:
insert into USER_INFO (ID,NAME,COUNTRY,DATE_TIME,TIMESTAMP) values(4,' Li Liu ','China',timestamp '2020-07-30 20:30:30',timestamp '2020-07-30 20:30:30.55555')

Query database system time
SELECT SYSDATE FROM DUAL
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual
--oracle We can't omit from keyword ,dual This watch is oracle My watch , So you can use
边栏推荐
- 大厂面试算法系列-动态规划求解最长公共子串问题
- How about opening an account for shares of tongdaxin? Is it safe to open an account?
- Multithreading - the concept of program process threads
- UML总结
- Fabric. JS activation input box
- Common classes - overview of string classes
- 容器部署和无服务器部署那些事儿
- 「冲刺大厂基础2」
- 蓝桥杯电子类单片机第十一届决赛试题
- Lvs+keepalived high availability
猜你喜欢

LVS+keepalived高可用

浅谈数据库优化(以mysql为例)

Application and analysis of special cases of data visualization

New occupation: digital management abbess is in hot demand. As long as she comes for an interview, the enterprise will make an offer

分布式消息中间件--消息队列介绍

蓝桥杯电子类单片机第十一届决赛试题

Simple analysis of performance inflection point

多线程-程序进程线程的概念

新职业:数字化管理师太抢手,只要来面试,企业就会发offer

error converting YAML to JSON: yaml: line 10: found character that cannot start any token
随机推荐
2022 hydrogenation process question bank and simulation test
使用OpenCore引导黑苹果
Common interview questions: Browser input domain name and open the completed process
Distributed message middleware -- Introduction to message queue
软件设计文档最容易忽略哪些?
2022 simulation test platform operation of gantry crane driver theory question bank
多余的时间不要浪费,玩玩手机可开启“副业人生”
纯英式 VoxEdit 大赛来啦
[SDU project training level 2019] router link: who will execute to or @click first
Realization of C 2~36 hexadecimal number conversion
A group of interesting pictures to explain the status code
Container deployment and serverless deployment
InfoWorld文章丨将数据编排技术用于AI模型训练
Some pitfalls encountered in using Renren open source project
pushmall推贴共享电商2022年5月份计划更新完成
js正则表达式匹配span标签
[SDU project training level 2019] personal home page display + personal summary
【6月第一周学习记录】UU-Computer vision(1):3D reconstruction&Camera calibration
Large factory interview algorithm series - dynamic programming for solving the longest common substring problem
Peripheral driver library development notes 42:dac8552 DAC driver