Oracle Skill Enhancement Part 7 Date Operations

1. Add or subtract day, month, year

2. Add and subtract hours, minutes, seconds

3. Date interval now, minutes, seconds

4. Date interval day, month, year

 

5. Count the number of days in the week of the year (for example, how many days are there on Monday, how many days are on Tuesday, etc.)

WITH x0 AS
 (SELECT trunc(SYSDATE, 'y') AS beginning of the year FROM dual),
x1 AS
 (SELECT beginning of the year, add_months(beginning of the year, 12) AS end of the year FROM x0),
x2 AS
 (SELECT beginning of the year, end of the year, year-end-beginning of the year AS days FROM x1),
x3 AS /*Generate list*/
 (SELECT beginning of the year + LEVEL - 1 AS date FROM x2 CONNECT BY LEVEL <= number of days),
x4 AS /*Rename the data*/
 (SELECT date, to_char(date, 'DY') AS Week FROM x3)
/*Totalize Days*/
SELECT Week, COUNT(*) AS Day FROM x4 GROUP BY Week;

 You can use 'DDD', to calculate the number of days, the statement is to_char((add_months(trunc(SYSDATE,'y'),12) - 1),'DDD')

 

6. Determine the number of days between the current record and the next record

Find the number of days between the employee and the next employee hired. Use lead to return the date of the next hire. 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324124542&siteId=291194637