First day, last date of week, last date of month, last date of quarter and last date of year using Oracle date function
Gets First day, last date of week, last date of month, last
date of quarter, last date of year using oracle date functions.
--First day of current week
SELECT TRUNC (SYSDATE, 'Day') first_day_of_current_week
FROM DUAL;
--First day of next week
SELECT TRUNC (SYSDATE + 7, 'Day') first_day_of_next_week
FROM DUAL;
--First day of previous week
SELECT TRUNC (SYSDATE - 7, 'Day') first_day_of_previous_week
FROM DUAL;
--First day of current month
SELECT TRUNC (SYSDATE, 'Month') first_day_of_current_month
FROM DUAL;
--First day of previous month
SELECT TRUNC (TRUNC (SYSDATE, 'Month') - 1, 'Month') first_day_of_previous_month
FROM DUAL;
--First day of next month
SELECT TRUNC (LAST_DAY (SYSDATE) + 1, 'Month') first_day_of_next_month
FROM DUAL;
--First day of current year
SELECT TRUNC (SYSDATE, 'Year') first_day_of_current_year
FROM DUAL;
--First day of previous year
SELECT TRUNC (TRUNC (SYSDATE, 'Year') - 1, 'Year') first_day_of_previous_year
FROM DUAL;
--First day of next year
SELECT ADD_MONTHS (TRUNC (SYSDATE, 'Year'), 12) first_day_of_next_year
FROM DUAL;
-- First Day of Current quater
SELECT TRUNC (SYSDATE, 'Q') first_day_of_current_quater
FROM DUAL;
-- First Day of Previous Quarter
SELECT ADD_MONTHS (TRUNC (SYSDATE, 'Q'), -3) first_day_of_previous_quarter
FROM DUAL;
-- First Day of Next Quarter
SELECT ADD_MONTHS (TRUNC (SYSDATE, 'Q'), 3) first_day_of_next_quarter
FROM DUAL;
--Last day of current week
SELECT TRUNC (SYSDATE, 'Day') + 6 last_day_of_current_week
FROM DUAL;
--Last day of next week
SELECT TRUNC (SYSDATE + 7, 'Day') + 6 last_day_of_next_week
FROM DUAL;
--Last day of previous week
SELECT TRUNC (SYSDATE - 7, 'Day') + 6 last_day_of_previous_week
FROM DUAL;
--Last day of current month
SELECT LAST_DAY (TRUNC (SYSDATE, 'Month')) last_day_of_current_month
FROM DUAL;
--Last day of previous month
SELECT LAST_DAY (TRUNC (TRUNC (SYSDATE, 'Month') - 1, 'Month')) last_day_of_previous_month
FROM DUAL;
--Last day of next month
SELECT LAST_DAY (TRUNC (LAST_DAY (SYSDATE) + 1, 'Month')) last_day_of_next_month
FROM DUAL;
--Last day of current year
SELECT LAST_DAY (ADD_MONTHS (TRUNC (SYSDATE, 'Year'), 11)) last_day_of_current_year
FROM DUAL;
--Last day of previous year
SELECT LAST_DAY (ADD_MONTHS (TRUNC (TRUNC (SYSDATE, 'Year') - 1, 'Year'), 11)) last_day_of_previous_year
FROM DUAL;
--Last day of next year
SELECT LAST_DAY (ADD_MONTHS (TRUNC (TRUNC (SYSDATE, 'Year') - 1, 'Year'), -13)) last_day_of_next_year
FROM DUAL;
-- Last Day of Current quater
SELECT LAST_DAY (ADD_MONTHS (TRUNC (SYSDATE, 'Q'), 2)) last_day_of_current_quater
FROM DUAL;
-- Last Day of Previous Quarter
SELECT TRUNC (SYSDATE, 'Q') - 1 last_day_of_previous_quarter
FROM DUAL;
-- Last Day of Next Quarter
SELECT LAST_DAY (ADD_MONTHS (TRUNC (SYSDATE, 'Q'), 5)) last_day_of_next_quarter
FROM DUAL;
-- Cheers !!!
Comments
Post a Comment