当前位置:网站首页>Conversion of Oracle date
Conversion of Oracle date
2022-07-27 20:28:00 【bigdata7】
to_date()
to_date(" String to convert "," Converted format ") The format of the two parameters must match , Otherwise, an error will be reported .
That is, the first parameter is interpreted according to the format of the second parameter .
to_char( date ," Transformation format " ) That is, according to the given date “ Transformation format ” transformation .
Converted format :
- Express year Of :y The last person of the year yy At the end of the year 2 position yyy At the end of the year 3 position yyyy use 4 The number of digits represents the year
- Express month Of :mm use 2 A digit represents the month ;mon In short form such as 11 Month or nov ;month In full name such as 11 Month or november
- Express day Of :dd It means the day of the month ;ddd The first day of the year ;dy What day of the week Abbreviation Like Friday or fri;day What day of the week Full write
Like Friday or friday. - Express hour Of :hh 2 The number of digits represents the hour 12 Base number ; hh24 2 The number of digits represents the hour 24 Hours
- Express minute Of :mi 2 The number of digits represents minutes
- Express second Of :ss 2 The number of digits represents seconds 60 Base number
- It's a quarter :q One digit It means quarter (1-4)
- And then there is ww Used to indicate the week of the year w Used to indicate the week of the month .
- 24 The time range in the hour system :00:00:00-23:59:59
- 12 The time range in the hour system :1:00:00-12:59:59
such as :
select to_char(sysdate,'yy-mm-dd hh24:mi:ss') from dual // Show :08-11-07 13:22:42
select to_date('2005-12-25,13:25:59','yyyy-mm-dd,hh24:mi:ss') from dual // Show :2005-12-25 13:25:59
And if you write in the above form :select to_date('2005-12-25,13:25:59','yyyy-mm-dd,hh:mi:ss') from dual, May be an error , Because hours hh yes 12 Base number ,13 For illegal input , Can't match .
Add :
Subtract... From the current time 7 Minutes of time
select sysdate,sysdate - interval '7' MINUTE from dual
Subtract... From the current time 7 Hours of time
select sysdate - interval '7' hour from dual
Subtract... From the current time 7 Time of day
select sysdate - interval ’7’ day from dual
Subtract... From the current time 7 Time of the month
select sysdate,sysdate - interval '7' month from dual
Subtract... From the current time 7 Years of time
select sysdate,sysdate - interval '7' year from dual
Multiply the time interval by a number
select sysdate,sysdate - 8*interval '7' hour from dual
Dual Pseudo column
Explanation of meaning :
Dual yes Oracle An actual table in , Any user can read , It's often used when there's no target table select In the block .
such as , I want to get system time , Then use “select sysdate from dual” Then return to the current time of the system :2008-11-07 9:32:49, Different systems may return dates in different formats .“select user from dual" Then return the currently connected user . If it is "select 1+2 from dual”, Returns the result :3
come from :http://hi.baidu.com/xkf007/blog/item/eb21d983b92e78b30cf4d2a5.html
TO_DATE Format ( In time :2007-11-02 13:45:25 For example )
Year:
yy two digits Two years Display value :07
yyy three digits Three years Display value :007
yyyy four digits Four years Display value :2007
Month:
mm number Two months Display value :11
mon abbreviated The character set represents Display value :11 month , If it's in English , Show nov
month spelled out The character set represents Display value :11 month , If it's in English , Show november
Day:
dd number What day of the month Display value :02
ddd number The first few days of that year Display value :02
dy abbreviated The day of the week Display value : Friday , If it's in English , Show fri
day spelled out What day of the week is it all written Display value : Friday , If it's in English , Show friday
ddspth spelled out, ordinal twelfth
Hour:
hh two digits 12 Hourly system Display value :01
hh24 two digits 24 Hourly system Display value :13
Minute:
mi two digits 60 Base number Display value :45
Second:
ss two digits 60 Base number Display value :25
Other
Q digit quarter Display value :4
WW digit In the first few weeks of that year Display value :44
W digit What week of the month Display value :1
24 The time range in the hour format is : 0:00:00 - 23:59:59....
12 The time range in the hour format is : 1:00:00 - 12:59:59 ....
- Date and character conversion function usage (to_date,to_char)
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; // The date is converted to a string
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
select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss') from dual//
select to_char( to_date(222,‘J’),‘Jsp’) from dual
Show Two Hundred Twenty-Two
3. Ask what day of the week it is
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;
Monday
select to_char(to_date('2002-08-26','yyyy-mm- dd'),'day','NLS_DATE_LANGUAGE = American') from dual;
monday
Set the date language
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
You can do that
TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')
- The number of days between two dates
select floor(sysdate - to_date(‘20020405’,‘yyyymmdd’)) from dual;
- Time is null Usage of
select id, active_date from table1
UNION
select 1, TO_DATE(null) from dual;
Pay attention to TO_DATE(null)
6. Month difference
a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')
that 12 month 31 At noon, 12 After two o'clock and 12 month 1 The no. 12 Before the point is not included in this range .
therefore , When time needs to be precise , Think to_char It's still necessary
- Date format conflict
The input format depends on what you install ORACLE The type of character set , such as : US7ASCII, date The type of format is : ‘01-Jan-01’
alter system set NLS_DATE_LANGUAGE = American
alter session set NLS_DATE_LANGUAGE = American
Or in to_date Write in
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;
Notice that I'm just raising NLS_DATE_LANGUAGE, Of course, there are a lot more ,
You can see
select * from nls_session_parameters
select * from V$NLS_PARAMETERS
select count(*)
from ( select rownum-1 rnum
from all_objects
where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002- 02-01','yyyy-mm-dd')+1
)
where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )
not in ( '1', '7' )
lookup 2002-02-28 to 2002-02-01 Days between Monday and seven
Call... Before and after DBMS_UTILITY.GET_TIME, Let's subtract the results from each other ( Get is 1/100 second , Not milliseconds ).
- Find the month
select months_between(to_date('01-31-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
1
select months_between(to_date('02-01-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
1.03225806451613
- Next_day Usage of
Next_day(date, day)
Monday-Sunday, for format code DAY
Mon-Sun, for format code DY
1-7, for format code D
11
select to_char(sysdate,'hh:mi:ss') TIME from all_objects
Be careful : The first record TIME It's the same as the last line
You can build a function to deal with this
create or replace function sys_date return date is
begin
return sysdate;
end;
select to_char(sys_date,'hh:mi:ss') from all_objects;
12. Get hours
extract() Find the field value of the date or interval value
SELECT EXTRACT(HOUR FROM TIMESTAMP ‘2001-02-16 2:38:40’) from offer
SQL> select sysdate ,to_char(sysdate,‘hh’) from dual;
SYSDATE TO_CHAR(SYSDATE,‘HH’)
2003-10-13 19:35:21 07
SQL> select sysdate ,to_char(sysdate,‘hh24’) from dual;
SYSDATE TO_CHAR(SYSDATE,‘HH24’)
2003-10-13 19:35:21 19
13. The date of processing
select older_date,
newer_date,
years,
months,
abs(
trunc(
newer_date-
add_months( older_date,years*12+months )
)
) days
from ( select
trunc(months_between( newer_date, older_date )/12) YEARS,
mod(trunc(months_between( newer_date, older_date )),12 ) MONTHS,
newer_date,
older_date
from (
select hiredate older_date, add_months(hiredate,rownum)+rownum newer_date
from emp
)
)
14. The way to deal with the uncertainty of months and days
select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual
16. Find out the number of days this year
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual
How to deal with leap years
to_char( last_day( to_date('02' | | :year,'mmyyyy') ), 'dd' )
If it is 28 It's not a leap year
17.yyyy And rrrr The difference between
'YYYY99 TO_C
------- ----
yyyy 99 0099
rrrr 99 1999
yyyy 01 0001
rrrr 01 2001
18. Processing of different time zones
select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdate
from dual;
19.5 Every second
Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,TO_CHAR(sysdate,'SSSSS')
from dual
2002-11-1 9:55:00 35786
SSSSS Express 5 Bit seconds
20. The day of the year
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual
310 2002-11-6 10:03:51
21. Calculating hours , branch , second , millisecond
select
Days,
A,
TRUNC(A*24) Hours,
TRUNC(A*24*60 - 60*TRUNC(A*24)) Minutes,
TRUNC(A*24*60*60 - 60*TRUNC(A*24*60)) Seconds,
TRUNC(A*24*60*60*100 - 100*TRUNC(A*24*60*60)) mSeconds
from
(
select
trunc(sysdate) Days,
sysdate - trunc(sysdate) A
from dual
)
select * from tabname
order by decode(mode,'FIFO',1,-1)*to_char(rq,'yyyymmddhh24miss');
//
floor((date2-date1) /365) As a year
floor((date2-date1, 365) /30) As a month
d(mod(date2-date1, 365), 30) As a day .
23.next_day function Return to next week's date ,day by 1-7 Or Sunday - Saturday ,1 Means Sunday
next_day(sysdate,6) It's the next Friday from now on . The following figures start from Sunday .
1 2 3 4 5 6 7
Japan One Two 3、 ... and Four 5、 ... and 6、 ... and
select (sysdate-to_date('2003-12-03 12:55:45','yyyy-mm-dd hh24:mi:ss'))*24*60*60 from ddual
date It's the day back then Convert to ss
24,round Round to the nearest date
select sysdate S1,
round(sysdate) S2 ,
round(sysdate,'year') YEAR,
round(sysdate,'month') MONTH ,
round(sysdate,'day') DAY from dual
25,trunc[ Truncate to the nearest date , The unit is days ] , The date type is returned
select sysdate S1,
trunc(sysdate) S2, // Return current date , No time, no minutes, no seconds
trunc(sysdate,'year') YEAR, // Return to... Of the current year 1 month 1 Japan , No time, no minutes, no seconds
trunc(sysdate,'month') MONTH , // Returns... Of the current month 1 Japan , No time, no minutes, no seconds
trunc(sysdate,'day') DAY // Return to the Sunday of the current week , No time, no minutes, no seconds
from dual
26, Returns the latest date in the date list
select greatest('01-1 month -04','04-1 month -04','10-2 month -04') from dual
27. Calculate the time difference
notes :oracle The time difference is in days , So, in terms of months , Japan
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from dual // Time difference - year
select ceil(moths_between(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanMonths from dual // Time difference - month
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanDays from dual // Time difference - God
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from dual // Time difference - when
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60) as spanMinutes from dual // Time difference - branch
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60*60) as spanSeconds from dual // Time difference - second
28. Update time
> notes :oracle Time addition and subtraction are in days , Let the change be n, So, in terms of months , Japan
select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss’),to_char(sysdate+n*365,‘yyyy-mm-dd hh24:mi:ss’) as newTime from dual // Change time - year
select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss’),add_months(sysdate,n) as newTime from dual // Change time - month
select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss’),to_char(sysdate+n,‘yyyy-mm-dd hh24:mi:ss’) as newTime from dual // Change time - Japan
select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss’),to_char(sysdate+n/24,‘yyyy-mm-dd hh24:mi:ss’) as newTime from dual // Change time - when
select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss’),to_char(sysdate+n/24/60,‘yyyy-mm-dd hh24:mi:ss’) as newTime from dual // Change time - branch
select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss’),to_char(sysdate+n/24/60/60,‘yyyy-mm-dd hh24:mi:ss’) as newTime from dual // Change time - second
29. Find the first day of the month , The last day
SELECT Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month,
Trunc(SYSDATE, 'MONTH') - 1 / 86400 Last_Day_Last_Month,
Trunc(SYSDATE, 'MONTH') First_Day_Cur_Month,
LAST_DAY(Trunc(SYSDATE, 'MONTH')) + 1 - 1 / 86400 Last_Day_Cur_Month
FROM dual;
边栏推荐
- Clickhouse 实现 MaterializedPostgreSQL
- 什么是多层感知机(什么是多层感知机)
- 速卖通:按关键字搜索商品 API
- Add joint control to gltf model
- Can software testing be learned in 2022? Don't learn, software testing positions are saturated
- PMP practice once a day | don't get lost in the exam -7.27 (including agility + multiple choices)
- Zepto入门详解
- Two years after its release, the price increased by $100, and the reverse growth of meta Quest 2
- Office automation solution - docuware cloud is a complete solution to migrate applications and processes to the cloud
- Libpcap library and pcap_ Sendpacket interface function understanding
猜你喜欢

Office automation solution - docuware cloud is a complete solution to migrate applications and processes to the cloud

Data warehouse construction - DWD floor

antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key

JS realizes video recording - Take cesium as an example

预处理与宏定义

技术分享 | 接口自动化测试中,如何做断言验证?

Add joint control to gltf model

Wu Hequan: digital technology empowering "double carbon" practice according to local conditions

Redis queue、rdb学习

How to run kevinchappell / FormBuilder
随机推荐
预处理与宏定义
Western digital mobile hard disk can't be read (the idiom of peace of mind)
Solve the problem of displaying the scroll bar when there is no data in the viewui table
Kubectl's access to pod logs -- the way to build a dream
Under the epidemic, I left my job for a year, and my income increased 10 times
《安富莱嵌入式周报》第275期:2022.07.18--2022.07.24
Add joint control to gltf model
JD: search product API by keyword
ZJNU 22-07-26 比赛心得
Preprocessing and macro definition
Redis queue、rdb学习
数仓搭建——DWD层
LG集团宣布将向湖北捐赠300万元现金、120万个口罩、1万套防护服
Product Manager: check where there is an error prompt of "system exception" on the offline
Cfssl of pki/tls tool -- the road to dream
Check the internship salary of Internet companies: with it, you can also enter the factory
内置函数其它函数
ES6 -- Application of expansion operator
C # network application programming, experiment 2: IP address translation and domain name resolution exercises
Redis-基本了解,五大基本数据类型