Page 1/2 of oracle date function set (centralized Version)

Source: Internet
Author: User
Tags date1 time zones
Oracle date functions are already available on the Internet. Let's focus on it to avoid your troubles.

Oracle date functions are already available on the Internet. Let's focus on it to avoid your troubles.

In the development of oracle databases, due to time issues, the weekly chapter is too large, so the date functions of ORACLE Data are specially added to favorites. It is for another day's query.
Add_months (d, n) date d plus n months
What month does last_day (d) contain d? The date of the last day
New_time (d, a, B)? Partition date and ?? D In B? Partition date and ??
Next_day (d, day) is more than the date d ?, Date of the week specified by day
Current sysdate system? Date and ??
Greatest (d1, d2,... dn )? The last date in the output date list
Least (d1, k2,... dn )? The earliest date in the output date list
To_char (d [, fmt]) date d is in the format specified by fmt ?? String
Is the to_date (st [, fmt]) string st in the format specified by fmt? Into date ?, If fmt is ignored, the st must use the default format.
Round (d [, fmt]) date d rounds to the latest date in the format specified by fmt
Trunc (d [, fmt]) date d is truncated to the latest date in the format specified by fmt
Convert to_date string type to date type
Character in the corresponding position in the string, must meet the time range limit

Query the Oracle Date Format
----------------------------------


Select * from nls_database_parameters;


The following table shows the date format in the table NLS_DATE_FORMAT.
PARAMETER VALUE
----------------------------------------------------------------------
NLS_LANGUAGE AMERICAN
NLS_TERRITORY AMERICA
NLS_CURRENCY $
NLS_ISO_CURRENCY AMERICA
NLS_NUMERIC_CHARACTERS .,
NLS_CHARACTERSET ZHS16GBK
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGE AMERICAN
NLS_SORT BINARY
NLS_TIME_FORMAT HH. MI. SSXFF AM
NLS_TIMESTAMP_FORMAT DD-MON-RR HH. MI. SSXFF AM
NLS_TIME_TZ_FORMAT HH. MI. ssxff am tzh: TZM
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR hh. MI. ssxff am tzh: TZM
NLS_DUAL_CURRENCY $
NLS_COMP BINARY
NLS_NCHAR_CHARACTERSET ZHS16GBK
NLS_RDBMS_VERSION 8.1.7.0.0

Or query the V $ NLS_PARAMETERS table,
Select * from V $ NLS_PARAMETERS;
There are similar results

SQL> select to_date ('2017-11-12 12-07-32 ', 'yyyy-mm-dd hh24-mi-ss') value from dual;
VALUE
-------------------
2004.11.12 12:07:32

SQL> select to_date ('20140901') value from dual;
VALUE
-------------------
2004.10.15 00:00:00

SQL> select to_date ('20140901') value from dual;
ERROR is located in row 1st:
ORA-01861: Text and format strings do not match


Sysdate current date and time

SQL> select sysdate value from dual;
VALUE
-------------------
2003.11.23 17:09:01


Last_day

SQL> select last_day (sysdate) value from dual;
VALUE
-------------------
2003.11.30 17:08:17


Add_months (d, n) date d is pushed n Months Later

SQL> select add_months (sysdate, 2) value from dual;
VALUE
-------------------
2005.01.23 17:10:21


What is the date of the specified day (the day of the week) in the first week after the next_day (d, day) Date

SQL> select next_day (sysdate, 1) value from dual;
VALUE
-------------------
2004.11.28 17:38:55




[Oracle/plsql] Full oracle date processing version

Date Processing Full Edition
TO_DATE format
Day:
Dd number 12
Dy abbreviated fri
Day spelled out Friday
Ddspth spelled out, ordinal twelfth
Month:
Mm number 03
Mon abbreviated mar
Month spelled out march
Year:
Yy two digits 98
Yyyy four digits 1998

The time range in the 24-hour format is: 0:00:00-23:59:59 ....
The time range in the 12-hour format is: 1:00:00-12:59:59 ....
1.
Date and character conversion function usage (to_date, to_char)

2.
Select to_char (to_date (222, 'J'), 'jsp ') from dual

Display Two Hundred Twenty-Two

3.
The day of the week.
Select to_char (to_date ('1970-08-26 ', 'yyyy-mm-dd'), 'day') from dual;
Monday
Select to_char (to_date ('1970-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 also
TO_DATE ('1970-08-26 ', 'yyyy-mm-dd', 'nls _ DATE_LANGUAGE = American ')

4.
Number of days in a two-day period
Select floor (sysdate-to_date ('20140901', 'yyyymmdd') from dual;

5. Use a time of null
Select id, active_date from table1
UNION
Select 1, TO_DATE (null) from dual;

Note that TO_DATE (null) is used)

6.
A_date between to_date ('20140901', 'yyyymmdd') and to_date ('20140901', 'yyyymmdd ')
Therefore, it is not included in this range after on January 1, December 31 and before on January 1, December 1.
Therefore, when the time needs to be accurate, to_char is still necessary.
7. Date Format conflict
The input format depends on the type of the ORACLE character set you installed, for example, US7ASCII. The date format is '01-Jan-01'
Alter system set NLS_DATE_LANGUAGE = American
Alter session set NLS_DATE_LANGUAGE = American
Or write it in to_date.
Select to_char (to_date ('1970-08-26 ', 'yyyy-mm-dd'), 'day', 'nls _ DATE_LANGUAGE = American') from

Dual;
Note that I just mentioned NLS_DATE_LANGUAGE. Of course there are many more,
Available
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 ('2017-02-28 ', 'yyyy-mm-dd')-to_date ('2017-
02-01 ', 'yyyy-mm-dd') + 1
)
Where to_char (to_date ('1970-02-01 ', 'yyyy-mm-dd') + rnum-1, 'd ')
Not
In ('1', '7 ')

Find the number of days between and except Monday and seven
Call DBMS_UTILITY.GET_TIME to subtract the result (1/100 seconds instead of milliseconds ).

9.
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 TIME of the first record is the same as that of the last record.
You can create a function to solve this 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;
12.
Hours

Select extract (hour from timestamp '2017-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 ')
-------------------------------------------
19:35:21 19

Obtain the year, month, and day.
13.
Processing of year, month, and day
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.
How to deal with the uncertainty of the number of days in a month
Select to_char (add_months (last_day (sysdate) + 1,-2), 'yyyymmdd'), last_day (sysdate) from dual

16.
Find the number of days this year
Select add_months (trunc (sysdate, 'Year'), 12)-trunc (sysdate, 'Year') from dual

How to deal with a leap year
To_char (last_day (to_date ('02 '|: year, 'mmyyyy'), 'dd ')
If it is 28, it is not a leap year.

17.
Difference between yyyy and rrrr
'Yyyy99 TO_C
-----------
Yyyy 99 0099
Rrrr 99 1999
Yyyy 01 0001
Rrrr 01 2001

18. Processing in different time zones
Select to_char (NEW_TIME (sysdate, 'gmt', 'est '), 'dd/mm/yyyy hh: mi: ss'), sysdate
From dual;

19.
One interval in five seconds
Select TO_DATE (FLOOR (TO_CHAR (sysdate, 'ssss')/300) * 300, 'ssss'), TO_CHAR (sysdate, 'ssss ')
From dual

2002-11-1 9:55:00 35786
SSSSS indicates five seconds

20.
The day of the year
Select TO_CHAR (SYSDATE, 'ddd '), sysdate from dual
310 10:03:51

21. Computing hour, minute, 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)
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
Mod (date2-date1, 365), 30) as daily.
23. next_day Function
Next_day (sysdate, 6) is the next Friday from the current start. The following number is counted from Sunday.

A simple sentence.
The Oracle date is treated as a special number in days.
Date + number = date, date-date = number, date-number = Date

The formatted display of MESTAMP data is the same as that of DATE data. Note that the to_char function supports date and timestamp, but trunc does not support the TIMESTAMP data type. This clearly shows that the use of TIMESTAMP data type is more accurate than the DATE data type when the difference between the two time is extremely important.
  
If you want to display the fractional seconds of TIMESTAMP, refer to the following:
  
1 SELECT TO_CHAR (time1, 'Mm/DD/YYYY HH24: MI: SS: ff3') "Date" FROM date_table
  
Date
  
-----------------------
  
06/20/2003 16: 55: 14: 000
  
06/26/2003 11: 16: 36: 000
  
In the preceding example, only three digits after the decimal point are allowed.
  
It is easier to calculate the data difference between timestamp than the old date data type. If you subtract it, you can see what will happen. The result is easier to understand, 17 days, 18 hours, 27 minutes, and 43 seconds in the first line.
  
1 SELECT time1,
  
2 time2,
  
3 substr (time2-time1), instr (time2-time1), '') +) seconds,
  
4 substr (time2-time1), instr (time2-time1), '') +) minutes,
  
5 substr (time2-time1), instr (time2-time1), '') +) hours,
  
6 trunc (to_number (substr (time2-time1), 1, instr (time2-time1, '') days,
  
7 trunc (to_number (substr (time2-time1), 1, instr (time2-time1, '')/7) weeks
  
8 * FROM date_table
  
TIME1 TIME2 SECONDS MINUTES HOURS DAYS WEEKS
  
-------------------------------------------------------------------------------
  
06/20/2003: 16: 55: 14: 000000 07/08/2003: 11: 22: 57: 000000 43 27 18 17 2
  
06/26/2003: 11: 16: 36: 000000 07/08/2003: 11: 22: 57: 000000 21 06 00 12 1
  
This means that you no longer need to worry about how many seconds a day is in troublesome computing. Therefore, getting the number of days, months, days, hours, minutes, And seconds is the use of the substr function to extract numbers.
  
System Date and Time
  
To obtain the system time, the Data Type of date is returned. You can use the sysdate function.
  
SQL> SELECT SYSDATE FROM DUAL;
  
To obtain the system time, the data type is returned as timestamp. You can use the aggregate impstamp function.
  
SQL> SELECT distinct imestamp FROM DUAL;
  
You can set the initialization parameter FIXED_DATE to specify the sysdate function to return a fixed value. This is used in code that is sensitive to test date and time. Note that this parameter is invalid for the systimestamp function.
  
SQL> ALTER SYSTEM SET fixed_date = '2017-01-01-10:00:00 ';
  
System altered.
  
SQL> select sysdate from dual;
  
SYSDATE
  
---------
  
01-JAN-03
  
SQL> select distinct imestamp from dual;
  
SYSTIMESTAMP
  
---------------------------------------------------------
  
09-JUL-03 11.05.02.519000 AM-06:00
  
When the date and timestamp types are used, the selection is clear. You can handle the date and timestamp types at will. When you try to convert to a more powerful timestamp, You need to note that they both have similarities and differences, which is enough to cause damage. The two have their own advantages in conciseness and interval size. Please choose a proper one.


DateDiff (month, waterpay. CopyDate, GetDate () = 1)

DateDiff (day, WaterPay. CopyDate, GetDate () = 1)
The meaning of these two sentences is: Calculate the record that returns the value of a date field (waterpay. CopyDate) in the database minus 1 in the form of month or day
How can I rewrite these two functions in SQL server in oracle?

Number of questions: 20. replies: 2
Top


Hevin (nothing is impossible) on the first floor replied to the first sentence with a score of 0 at 18:12:10:
Months_Between (to_date (to_char (sysdate, 'yyyy-mm-dd'), 'yyyy-mm-dd '),
To_date (to_char (waterpay. CopyDate, 'yyyy-mm-dd'), 'yyyy-mm-dd ')
) = 1
Top

Hevin on the second floor (nothing is impossible) replied to the second sentence with a score of 20 at 18:18:06:
To_char (sysdate-1, 'yyyy-mm-dd') = to_char (waterpay. CopyDate, 'yyyy-mm-dd ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.