1. select trunc (sysdate) from dual -- 2011-3-18 today's date is 2011-3-18 2. select trunc (sysdate, 'mm') from dual -- 2011-3-1 returns the first day of the current month. 3. select trunc (sysdate, 'yy') from dual -- 2011-1-1 return the first day of the current year 4. select trunc (sysdate, 'dd') from dual -- 2011-3-18 return current year month day 5. select trunc (sysdate, 'yyyy') from dual -- 2011-1-1 return the first day of the current year 6. select trunc (sysdate, 'D') from dual -- 2011-3-13 (Sunday) returns the first day of the current week 7. select trunc (sysdate, 'hh') from dual -- 2011-3-18 14:00:00 current time is 8. select trunc (sysdate, 'mi') from dual -- 2011-3-18 14:41:00 the TRUNC () function has no precision in seconds. ********************/ /* TRUNC (number, num_digits) Number. Num_digits is used to specify the number to take an integer. The default value of Num_digits is 0. When TRUNC () function is intercepted, No rounding is performed. */ 9. select trunc (123.458) from dual -- 123 10. select trunc (123.458, 0) from dual -- 123 11. select trunc (123.458, 1) from dual -- 123.4 12. select trunc (123.458,-1) from dual -- 120 13. select trunc (123.458,-4) from dual -- 0 14. select trunc (123.458, 4) from dual -- 123.458 15. select trunc (123) from dual -- 123 16. select trunc (123) from dual -- 17. select trunc (123,-1) from dual -- 120 Select to_char (sysdate, 'yyyy-mm-dd hh24: mi: ss') from dual; -- display the current time 2011-12-29 16:24:34 Select trunc (sysdate, 'Year') from dual; -- truncated to the year (the first day of this year) 2011-1-1 Select trunc (sysdate, 'q') from dual; -- truncated to the quarter (the first day of the quarter) 2011-10-1 Select trunc (sysdate, 'month') from dual; -- intercept to month (the first day of this month) 2011-12-1 Select trunc (sysdate, '') from dual; Null Select to_char (trunc (sysdate), 'yyyymmdd hh24: mi: ss') from dual; -- intercepted to the day by default (00:00:00 Of The Day) 00:00:00 20111229 Select trunc (sysdate-1, 'w') from dual; -- Thursday closest to the current time, returns the day if it is Thursday; Otherwise returns the last Thursday 2011-12-22 Select trunc (sysdate, 'ww ') from dual; -- intercepted last weekend (Saturday last week) 2011-12-24 Select trunc (sysdate, 'day') from dual; -- intercept to week (the first day of the week, that is, last Sunday) 2011-12-25 Select trunc (sysdate, 'iw') from dual; -- 2nd days of the week, that is, this Monday 2011-12-26 Select to_char (trunc (sysdate, 'dd'), 'yyyymmdd hh24: mi: ss') from dual; -- truncated to the day (00:00:00 of the current day) 00:00:00 20111229 Select trunc (sysdate, 'hh24') from dual; -- truncated to the hour (current hour, 0 minutes 0 seconds) 2011-12-29 16:00:00 Select trunc (sysdate, 'mi') from dual; -- truncated to minute (current minute, zero second) 2011-12-29 16:24:00 Select trunc (sysdate, 'ss') from dual; -- an error is reported, which is not accurate to the second format. |