Oracle trunc () function

Source: Internet
Author: User
Tags julian day

The above is reprinted, and most of them are not verified.

Select trunc (sysdate, 'dd') from dual. The result is: 12:00:00, which is different from the following!

 

Oracle trunc () function usage

 

1. trunc (for dates)
The date value intercepted by the trunc function for the specified element.
The syntax format is as follows:
Trunc (date [, FMT])
Where:
Date: A date value.
FMT date format, which is truncated by the specified Element format. Ignore it and it is intercepted by the latest date.
The usage of this function is as follows:
Trunc (to_date ('24-Nov-1999 PM ', 'dd-mon-yyyy hh: Mi am '))
= '24-Nov-1999 12:00:00 am'
Trunc (to_date ('24-Nov-1999 08:00:00 PM ', 'dd-mon-yyyy hh: Mi am', 'hh') = '24-Nov-1999 am'

When no format is specified for round (date, ''format''), if the time in the date is before noon, the time in the date is truncated to 12. m. (midnight, the beginning of a day), or enter the next day.

If format is not specified for trunc (date, ''format''), the date is cut to 12 A. M., and the condition before noon is not considered.

2. trunc (for number)
The trunc function returns the processed value. Its working mechanism is very similar to that of the round function, except that the function does not round or select the part before or after the specified decimal number.
The syntax format is as follows:
Trunc (number [, decimals])
Where:
Number the value to be intercepted
Decimals indicates the number of digits after the decimal point to be retained. Optional. If this parameter is ignored, all decimal parts are truncated.
The usage of this function is as follows:
Trunc (89.985, 2) = 89.98
Trunc (89.985) = 89
Trunc (89.985,-1) = 80
Note: The second parameter can be a negative number, indicating that the part after the specified number of digits on the left of the decimal point is truncated.

{

When format is set to day, It is accurate to the day, regardless of the number of months in a few years as long as it is the correct day, to determine a day in a year, you need to use trunc (date, ''dd '').

To put it bluntly, the format is year, accurate to ----- year.

For months, accurate to ------ year, month (no matter which year, as long as it is the same month and day)
Is the day, accurate to ------ year, month, day (regardless of which month, only care about the day)

}

//////////////////////////////////////// ///////////////////////////////////////

 

Instance:

For numbers and dates
SQL> select trunc (sysdate) from dual;

Trunc (sysd
----------
Month-03

SQL> select trunc (sysdate, 'mm') from dual;

Trunc (sysd
----------
01-1-03

SQL> select trunc (sysdate, 'yy') from dual;

Trunc (sysd
----------
01-1-03

SQL> select trunc (234.5565) from dual;

Trunc (234.5565)
---------------
234

SQL> select trunc (sysdate, 'D') from dual;

Trunc (sysd
----------
05-1-03

//////////////////////////////////////// ////////////////////////////////////

Select trunc (sysdate, 'dd') from dual; -- 2007-9-19

Select trunc (sysdate, 'yyyy') from dual; -- 2007-1-1

Select trunc (sysdate, 'mm') from dual; -- 2007-9-1

Begin
Dbms_output.put_line (to_char (sysdate), 'yyyy-mm-dd hh24: MI: ss '));
Dbms_output.put_line (to_char (sysdate) + 1/24/60/10, 'yyyy-mm-dd hh24: MI: ss '));
Dbms_output.put_line (to_char (sysdate) + 10/(24*60*60), 'yyyy-mm-dd hh24: MI: ss '));

Dbms_output.put_line (to_char (trunc (sysdate) + 10/(24*60*60), 'yyyy-mm-dd hh24: MI: ss '));

End;

Begin
Dbms_output.put_line ('current Time ');
Dbms_output.put_line (to_char (sysdate), 'yyyy-mm-dd hh24: MI: ss '));

Dbms_output.put_line ('current time + 1 s ');
Dbms_output.put_line (to_char (sysdate) + (1/24)/60)/60), 'yyyy-mm-dd hh24: MI: ss '));

Dbms_output.put_line ('current time + 1 s ');
Dbms_output.put_line (to_char (sysdate) + (5/24)/60)/60), 'yyyy-mm-dd hh24: MI: ss '));

Dbms_output.put_line ('current time + 10 s ');
Dbms_output.put_line (to_char (sysdate) + (10/(24*60*60), 'yyyy-mm-dd hh24: MI: ss '));

Dbms_output.put_line ('current Day ');
Dbms_output.put_line (to_char (trunc (sysdate), 'yyyy-mm-dd hh24: MI: ss '));

Dbms_output.put_line ('current 2nd days ');
Dbms_output.put_line (to_char (trunc (sysdate) + (1 + 1/24), 'yyyy-mm-dd hh24: MI: ss '));

Dbms_output.put_line ('current, 2nd days ');
Dbms_output.put_line (to_char (trunc (sysdate) + (1 + 9/24), 'yyyy-mm-dd hh24: MI: ss '));

End;
/
//////////////////////////////////////// ////////////////////////////////////

 

Common Oracle date functions (sysdate, Date Format)

1 sysdate
2 -- ◎ obtain the current system time
3
4 ex.
5 select sysdate from dual;
6
7 sysdate
8 ----------
9 20-sep-07
10
11 common date formats
12
13. Date Format description
14 ------------------------------------------------------------------------
15 yyyy/mm/DD-year/month/day
16 yyyy-year (4 digits)
17 YYY -- year (3 digits)
18 YY-year (2 digits)
19mm -- month
20 dd -- Date
21 d -- week
22 -- Sunday = 1 Monday = 2 Tuesday = 3
23 -- Wednesday = 4 Thursday = 5 Friday = 6 Saturday = 7
24
25 ddd-day of the year
26 Ww -- the second day of the year
27 w -- the fifth largest region in January
28 yyyy/mm/DD hh24: MI: ss -- year/month/day (24 hours): minute: Second
29 yyyy/mm/dd hh: MI: ss -- year/month/day (not in 24 hours): minute: Second
30 J -- Julian day, BC 4712/01/01 is 1
31 RR/MM/DD-2000 question
32 -- 00-49 = lower generation time; 50-99 = lower generation time
33 ex.
34 select to_char (sysdate, 'yyyy/MM/dd') from dual; -- 2007/09/20
35 select to_char (sysdate, 'yyyy') from dual; -- 2007
36 select to_char (sysdate, 'yyy') from dual; -- 007
37 select to_char (sysdate, 'yy') from dual; -- 07
38 select to_char (sysdate, 'mm') from dual; -- 09
39 select to_char (sysdate, 'dd') from dual; -- 20
40 select to_char (sysdate, 'D') from dual; -- 5
41 select to_char (sysdate, 'ddd ') from dual; -- 263
42 select to_char (sysdate, 'ww ') from dual; -- 38
43 select to_char (sysdate, 'w') from dual; -- 3
44 select to_char (sysdate, 'yyyy/MM/DD hh24: MI: ss') from dual; -- 15:24:13
45 select to_char (sysdate, 'yyyy/MM/dd hh: MI: ss') from dual; -- 03:25:23
46 select to_char (sysdate, 'J') from dual; -- 2454364
47 select to_char (sysdate, 'rr/MM/dd') from dual; -- 07/09/20
//////////////////////////////////////// ////////////////////////////////////

 

Related Article

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.