--Query current time
select sysdate from dual
--Result 2017/2/20 10:30:33 --Add
and subtract time --Add and subtract
year
select sysdate,add_months(sysdate,12) from dual
select sysdate,add_months( sysdate,-12) from dual --addition
and subtraction of months
select sysdate,add_months(sysdate,1) from dual
select sysdate,add_months(sysdate,-1) from dual --addition
and subtraction of weeks
select sysdate + 7 from dual
select sysdate - 7 from dual --Add
and subtract days
select sysdate + 1 from dual
select sysdate - 1 from dual --Add
and subtract hours
select sysdate + 1/24 from dual
select sysdate - 1/24 from dual --Add
and subtract minutes
select sysdate + 1/24/60 from dual
select sysdate - 1/24/60 from dual
--Add and subtract seconds
select sysdate + 1/24/60/60 from dual
select sysdate - 1/24/60/60 from dual --time
format
to_char Convert the time to a string in the specified format
select to_char(sysdate , 'yyyy-mm-dd hh24:mi:ss') from dual
--'yyyy-mm-dd hh24:mi:ss' formats the time in this format
--and so on --
' yyyy'year--
'q' quarter
--'mm' month
--'w' the week of the month (note: it starts from Sunday)
--'ww' the week of the year
--'d' the week of the week Day (Note: Sunday is the first day)
--'dd' the day of the month
--'ddd' the day of the year
-- 'hh' hour
-- 'mi' minute
-- 'ss' second
--'month'Chinese display month
--'day' Display the day of the week in Chinese
-- get the time in the early morning of today
select trunc(sysdate) from dual
-- get the time in the last second of today
select trunc(sysdate) + 0.99999 from dual
--Get the time at 4 o'clock today
select trunc(sysdate) + 4/24 from dual --The
time on the first day of the month
select trunc(sysdate,'mm') from dual --The
date on the first day of the next month
select trunc(add_months (sysdate,1),'mm') from dual --returns
the last day of the current month
select last_day(sysdate) from dual;
select last_day(trunc(sysdate)) from dual;
select trunc(last_day(sysdate)) from dual;
select trunc(add_months(sysdate,1),'mm') - 1 from dual; --get
every day of the year
select trunc(sysdate,'yyyy')+ rn -1 date0
from
(select rownum rn from all_objects
where rownum <366); --Today
is the Nth day of the year
SELECT TO_CHAR(SYSDATE,'DDD') FROM DUAL; --How
to add 2 years to an existing date
select add_months(sysdate,24) from dual;
--Determine whether the year of a certain day is a run year
select decode(to_char(last_day(trunc(sysdate,'y')+31),'dd'),'29','leap year','normal year') from dual; --Determine
whether it is a run year after two years
select decode(to_char(last_day(trunc(add_months(sysdate,24),'y')+31),'dd'),'29','leap year',' Normal year') from dual; --get
the quarter of the date
select ceil(to_number(to_char(sysdate,'mm'))/3) from dual;
select to_char(sysdate, 'Q') from dual
TO_DATE format (in time: 2007 -11-02 13:45:25 for example)
Year:
yy two digits year Display value: 07
yyy three digits year Display value: 007
yyyy four digits four digits year Display value: 2007
Month:
mm number Two-digit month Display value: 11
mon abbreviated character set representation Display value: November, if it is in English, display nov
month spelled out Character set representation Display value: November, if it is in English, display november
Day:
dd number The day of the month Display value: 02
ddd number The current year Display value of day: 02
dy abbreviated Abbreviated day of the week Display value: Friday, if English version, display fri
day spelled out full day of the week Display value: Friday, if English version, display friday
ddspth spelled out , ordinal twelfth
Hour:
hh two digits 12-hour base Display value: 01
hh24 two digits 24-hour base Display value: 13
Minute:
mi two digits 60 base Display value: 45
Second:
ss two digits Hexadecimal Display value: 25
Other
Q digit Quarter Display value: 4
WW digit Week of the year Display value: 44
W digit Week of the month Display value: 1
The time range in 24-hour format is: 0:00: 00 - 23:59:59....
The time range in 12-hour format is: 1:00:00 - 12:59:59 ....
1. Date and character conversion function usage (to_date,to_char)
select to_char( sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; //convert date to string
select to_char(sysdate,'yyyy') as nowYear from dual; //get the year of time
select to_char( sysdate,'mm') as nowMonth from dual; //get the month of the timeselect
to_char(sysdate,'dd') as nowDay from dual; //get the day of the time
select to_char(sysdate,'hh24') as nowHour from dual; //get time
select to_char(sysdate,'mi') as nowMinute from dual; //get time minutes
select to_char(sysdate,'ss') as nowSecond from dual; //Get the seconds of the time
select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss') from dual//
2.
select to_char( to_date( 222,'J'),'Jsp') from dual
display Two Hundred Twenty-Two
3. Find the day of the week
select to_char(to_date('2002-08-26','yyyy-mm-dd'),' day') from dual;
Mondayselect
to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;
monday
set date language
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
can also do this
TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')
4. The number of days between two days
select floor(sysdate - to_date ('20020405','yyyymmdd')) from dual;
5. The usage of time is null
select id, active_date from table1
UNION
select 1, TO_DATE(null) from dual;
Note to use TO_DATE(null)
6. Month difference
a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')
Then after 12:00 noon on December 31 and before 12:00 on December 1 are not included in this range.
Therefore, when the time needs to be precise, I think to_char is still necessary
7.
alter system set NLS_DATE_LANGUAGE = American
alter session set NLS_DATE_LANGUAGE = American
or write in to_date
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;
note that I only mentioned NLS_DATE_LANGUAGE, of course there are many more,
see
select * from nls_session_parameters
select * from V$NLS_PARAMETERS
8.
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' )
finds 2002-02-28 to 2002- Call DBMS_UTILITY.GET_TIME before and after the number of days between 02-01 except Monday and July, and
then subtract the results (the result is 1/100 of a second, not a millisecond).
9. 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
10. Usage of
Next_day 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
Note: The TIME of the first record is the same as the last row.
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 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.年月日的处理
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 indeterminate number of days in the month
select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd') ,last_day(sysdate) from dual
16. Find the number of days in this year
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual
leap year processing method
to_char( last_day( to_date(' 02' | | :year,'mmyyyy') ), 'dd' )
If it is 28, it is not a leap year
17. The difference between yyyy and
rrrr'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 seconds an interval
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 represents 5-digit seconds
20. The day of the year
select TO_CHAR(SYSDATE, 'DDD'), sysdate from dual
310 2002-11-6 10:03:51
21. Calculate hours, minutes, seconds, milliseconds
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 year
floor((date2-date1, 365) /30) as month
d(mod(date2- date1, 365), 30) as day.
23.next_day function returns the date of the next week, day is 1-7 or Sunday-Saturday, 1 means Sunday
next_day(sysdate,6) is the next Friday from the current one. The numbers that follow are from Sunday.
1 2 3 4 5 6 7th
123456
------------------------------------ ---------------------------
select (sysdate-to_date('2003-12-03 12:55:45','yyyy-mm -dd hh24:mi:ss'))*24*60*60 from ddual
date returns days and then converts to ss
24, round[round to nearest date] (day: round to nearest Sunday)
select sysdate S1,
round(sysdate) S2 ,
round(sysdate,'year') YEAR,
round(sysdate,'month') MONTH ,
round(sysdate,'day') DAY from dual
25,trunc[truncated to nearest Date, the unit is days], the return is the date type
select sysdate S1,
trunc(sysdate) S2, //return the current date, no hours, minutes and seconds
trunc(sysdate,'year') YEAR, //returns January 1st of the current year, no hours, minutes and seconds
trunc(sysdate,'month') MONTH, //returns the 1st day of the current month, no hours, minutes and seconds
trunc(sysdate, 'day') DAY //Return the Sunday of the current week, no hours, minutes and seconds
from dual
26, return the latest date in the date list
select greatest('01-January-04','04-January-04','10 -February-04') from dual
27. Calculate the time difference
Note: The oracle time difference is in days, so convert to adult months, days
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 - days
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-hour
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-minute
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
Note: Oracle time addition and subtraction is based on the number of days, and the change amount is set to n, so convert the adult month, day
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-day
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-hour
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-minute
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 and last day of the month
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;
转自:http://blog.csdn.net/foxbryant/article/details/6818670
oracle time operation and format conversion
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326370165&siteId=291194637
Recommended
Ranking