Usage of TO_DATE in Oracle

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
TO_DATEType (in terms of time: 2007-11-02 13:45:25)

Year:
Display YY two digits two yearsvalue:07
Display YYY three digits three yearsvalue:007
Display yyyy four digits four yearsvalue:2007

Month:
Display mm number two monthsvalue:11
Mon abbreviated character set to display value: November, if English version, Nov display
Month spelled out character set to display value: November, if English version, November display

Day:
DD number the first few days showvalue:02
DDD number in the first few days showvalue:02
Dy abbreviated when Zhou Di several days or display value: Friday, if English version, Fri display
Day spelled out this week in Tianquan write display: value Friday, if English version, Friday display
ddspth spelled out, ordinal twelfth

Hour:
HH two digits 12 hours hexadecimal displayvalue:01
Hh24 two digits 24 hours hexadecimal displayvalue:13

Minute:
Mi two digits 60 hexadecimal displayvalue:45

Second:
SS two digits 60 hexadecimal displayvalue:25

Other
Q digit quarter displayvalue:4
WW digit in the first few weeks showvalue:44
W digit the first few weeks of displayvalue:1

24 hours of time for the range;: 0:00:00 - 23:59:59....
12 hours of time for the range;: 1:00:00 - 12:59:59 ....

1 date and character conversion function.(to_date,to_char)

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; //Date into a string
select to_char(sysdate,'yyyy') as nowYear from dual; //The acquisition time of year
select to_char(sysdate,'mm') as nowMonth from dual; //The acquisition time of month
select to_char(sysdate,'dd') as nowDay from dual; //The acquisition time of day
select to_char(sysdate,'hh24') as nowHour from dual; //Access time
select to_char(sysdate,'mi') as nowMinute from dual; //Time access points
select to_char(sysdate,'ss') as nowSecond from dual; //The acquisition time of seconds
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 for the day is the day of the week
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';
So it can also be
TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')
The period of 4 days between two dates.
select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
The 5 time was null.
select id, active_date from table1
UNION
select 1, TO_DATE(null) from dual;

Note that to use TO_DATE(null)
6 month difference
a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')
Then in December 31st 12 points before noon after 12 and December 1st are not included in this range.
So, when the time need accurate time, think to_char is necessary

7 date conflict problem
Input type depends on the type, you install the ORACLE character set such as: US7ASCII, date type is of type: '01-Jan-01'
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 this is just NLS_DATE_LANGUAGE, of course, there are many,
To view
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' )

Find the 2002-02-28 to 2002-02-01 except Monday and seven days
In both before and after the call DBMS_UTILITY.GET_TIME, that will be the result of subtraction (get is 1/100 seconds, not milliseconds).
Find the 9 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 Next_day usage
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 first record of TIME is the same with the last line
A function is established to deal with the problem
create or replace function sys_date return date is
begin
return sysdate;
end;

select to_char(sys_date,'hh:mi:ss') from all_objects;

For 12 hours
extract()Find the date or the interval of value field;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 date
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
)
)
Methods 14 the number of days in the uncertain processing
select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual
16 days to find out this year
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual
Processing method of leap year
to_char( last_day( to_date('02' | | :year,'mmyyyy') ), 'dd' )
If 28 is not a leap year
The difference between 17.yyyy and rrrr
'YYYY99 TO_C
------- ----
yyyy 99 0099
rrrr 99 1999
yyyy 01 0001
rrrr 01 2001
18 different time zones
select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdate
from dual;
An interval of 19.5 seconds
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 said 5 seconds
20 a year for several days
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual

310 2002-11-6 10:03:51
21 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 the years
floor((date2-date1, 365) /30) As the month
d(mod(date2-date1, 365), 30)As the day.
The 23.next_day function returns the next week to date, day is 1-7 or Sunday - Saturday, said Sunday the 1
next_day(sysdate,6)From the beginning the next Friday. The latter figure is from Sunday onwards.
1 2 3 4 5 6 7
Day one two three four five six

---------------------------------------------------------------

select (sysdate-to_date('2003-12-03 12:55:45','yyyy-mm-dd hh24:mi:ss'))*24*60*60 from ddual
The date of return is then converted to SS

24,Round[rounded to the nearest date] (day: rounded to the 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 the nearest date, unit for the day, the return is the date type
select sysdate S1,
trunc(sysdate) S2, //Returns the current date, not when the minutes and seconds
trunc(sysdate,'year') YEAR, //Returns the current year January 1st, not when the minutes and seconds
trunc(sysdate,'month') MONTH , //Returns the current month 1 days, not when the minutes and seconds
trunc(sysdate,'day') DAY //Returns the current week's Sunday, not when the minutes and seconds
from dual
26,Returns the date list the latest date
select greatest('01-1 -04','04-1 -04','10-2 -04') from dual
27 calculating the time difference
Note: Oracle time difference is to stay as a unit, so the conversion of adult month, day

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 - years
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 - day
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 time
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 - Division
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 //The time difference.
The 28 update time
Note: Oracle time addition and subtraction is by days as a unit, a change amount is n, so the conversion of 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 the time - years
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),add_months(sysdate,n) as newTime from dual //Change the 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 the 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 the 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 the time - divided
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 the time in seconds
The first day of the 29 month of the search, 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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Jeremy at December 06, 2013 - 5:57 AM