How to get Month, Year and Days in postgresql between two dates - sql

I have two columns in the sql table which is startdate and enddate
Startdate Enddate
27-12-2015 22:30 03-01-2016 19:30
01-01-2016 12:45 09-02-2016 18:30
I want to get the resultant table like
Startdate Enddate Month year days
27-12-2015 22:30 03-01-2016 19:30 Dec 2015 5
27-12-2015 22:30 03-01-2016 19:30 Jan 2016 3
01-01-2016 12:45 09-02-2016 18:30 Jan 2016 31
01-01-2016 12:45 09-02-2016 18:30 Feb 2016 9

A rough solution would be to generate all the days and then aggregate (count) them. It works, but it's rough on memory. If it's not crucial, this solution would definitely work. The alternative is to generate a months series and make a day diff with a lot of conditions, if performance is critical.
SELECT
dates.startdate::DATE,
dates.enddate::DATE,
to_char(days.s, 'Mon') AS mon,
to_char(days.s, 'YYYY') AS yr,
count(1) AS d
FROM dates
CROSS JOIN LATERAL (
SELECT * FROM generate_series(dates.startdate, dates.enddate, INTERVAL '1 day') s
) days
GROUP BY 1, 2, 3, 4
In any case, here is the second variant, that loops through months instead (faster, harder to understand):
SELECT
dates.startdate::DATE,
dates.enddate::DATE,
to_char(months.startdate, 'Mon') AS mon,
to_char(months.startdate, 'YYYY') AS yr,
least(
months.enddate::DATE - dates.startdate::DATE + 1, -- takes care of first month
dates.enddate::DATE - months.startdate::DATE + 1, -- takes care of last month
months.enddate::DATE - months.startdate::DATE + 1 -- takes care of full months from the middle of the intervals
) AS "days"
FROM dates
-- get months as first day in that month
CROSS JOIN LATERAL (
SELECT * FROM generate_series(
(to_char(dates.startdate, 'YYYY-MM-') || '01')::DATE,
(to_char(dates.enddate + INTERVAL '1 month', 'YYYY-MM-') || '01')::DATE - 1, INTERVAL '1 month') m
) days
-- get months as start date and end date
CROSS JOIN LATERAL (
SELECT
days.m::DATE AS startdate,
(days.m + INTERVAL '1 month')::DATE - 1 AS enddate
) months

In this particular case a plpgsql function can provide better performance than a plain sql query.
create or replace function get_months(startdate date, enddate date)
returns table (mon text, year int, days int)
language plpgsql as $$
declare d date;
begin
d:= date_trunc('month', startdate);
while d < enddate loop
mon:= to_char(d, 'Mon');
year:= to_char(d, 'YYYY');
days:= case
when d+ '1month'::interval > enddate then enddate- d+ 1
when d < startdate then (d+ '1month'::interval)::date- startdate
else (d+ '1month'::interval)::date- d
end;
return next;
d:= d+ '1month'::interval;
end loop;
end
$$;
Test:
with my_table(startdate, enddate) as (
values
('2015-12-27 22:30', '2016-01-03 19:30'),
('2016-01-01 12:45', '2016-02-09 18:30')
)
select *
from my_table,
lateral get_months(startdate::date, enddate::date)
startdate | enddate | mon | year | days
------------------+------------------+-----+------+------
2015-12-27 22:30 | 2016-01-03 19:30 | Dec | 2015 | 5
2015-12-27 22:30 | 2016-01-03 19:30 | Jan | 2016 | 3
2016-01-01 12:45 | 2016-02-09 18:30 | Jan | 2016 | 31
2016-01-01 12:45 | 2016-02-09 18:30 | Feb | 2016 | 9
(4 rows)

Related

How to count number of people for each month, who was on internship, if we know dates of internship

First of all, sorry for bad title - I can't figure out how to write generalized formulation of my problem.
I have a table in PostgreSQL with users and dates of their internships. It looks like this:
user_id
start
end
1
December 22, 2019
June 29, 2020
2
March 8, 2020
September 8, 2020
3
May 21, 2020
November 21, 2020
From this I need to calculate for each month, how many people were on internship during this month. I only need to calculate full months (if internship actually started on December 22, 2019, I will calculate from January 2022. If internship were finished at June 29, 2020, I will calculate till May 2020.
Finally I need this table:
Month
Count
Jan-20
1
Feb-20
1
Mar-20
1
Apr-20
2
May-20
2
Jun-20
2
Jul-20
2
Aug-20
2
Sep-20
1
Oct-20
1
For making it absolutely clear, this is how I got it:
Month
user_1
user_2
user_3
Count
Jan-20
1
1
Feb-20
1
1
Mar-20
1
1
Apr-20
1
1
2
May-20
1
1
2
Jun-20
1
1
2
Jul-20
1
1
2
Aug-20
1
1
2
Sep-20
1
1
Oct-20
1
1
My idea is to:
Reshape my initial table, so it will look like this:
user_id
date
event
1
December 22, 2019
start
1
June 29, 2020
end
2
March 8, 2020
start
2
September 8, 2020
end
3
May 21, 2020
start
3
November 21, 2020
end
Generate series between each start and end event:
user_id
month
1
Jan-20
1
Feb-20
1
Mar-20
1
Apr-20
1
May-20
2
Apr-20
2
May-20
2
Jun-20
2
Jul-20
2
Aug-20
3
Jun-20
3
Jul-20
3
Aug-20
3
Sep-20
3
Oct-20
Using count() GROUP BY month
Unfortunately, I have problems with 1 and 2 clauses.
I don't know how to reshape the table in PostgreSQL. In Pandas I would use 'stack' function. For my case I can't find the appropriate function.
Even if I can reshape it, I don't understand how to make series of month for each user (shown above).
Please advise, what can be done here to solve my problem?
this query should do the job considering your table as test :
SELECT to_char(d.date, 'Mon-YY') AS month, count(*) AS count
FROM
( SELECT generate_series(date_trunc('month', min(start_date)), date_trunc('month', max(end_date)), interval '1 month') :: date AS date
FROM test
) AS d
INNER JOIN test AS t
ON daterange(t.start_date, t.end_date, '[]') && daterange(d.date, (d.date + interval '1 month') :: date)
WHERE daterange(t.start_date, t.end_date, '[]') #> daterange(d.date, (d.date + interval '1 month') :: date)
GROUP BY d.date
The first sub query calculate the months covered in table test.
The JOIN clause calculates the intersections between the months and the date interval for each user
The WHERE clause filters the rows where the date interval for a user corresponds to a full months.
Result :
month
count
Jan-20
1
Feb-20
1
Mar-20
1
Apr-20
2
May-20
2
Jun-20
2
Jul-20
2
Aug-20
2
Sep-20
1
Oct-20
1
see results in dbfiddle
Here is how I accomplished this:
Generated a calendar table using generate_series
Truncated the start and end dates to ensure we are only including instances where a full month of the internship was completed.
Performed a cross join to generate a cartesian product set.
Finally, add the WHERE predicate to include instances where the date_mm is between the truncated start and end dates.
SQL:
SELECT a.date_mm AS MONTH,
count(b.user_id) AS COUNT
FROM
(SELECT date_mm :: date
FROM generate_series('2020-01-01', '2023-01-01', '1 month' :: interval) date_mm) a
CROSS JOIN
(SELECT a.user_id,
a.start,
/* start_next_fom = first day of next month */
(date_trunc('month', a.start) + interval '1 month') AS start_next_fom,
a.end,
/* end_last_eom = last day of last month */
(date_trunc('month', a.end) - interval '1 day') AS end_last_eom
FROM users a) b
WHERE a.date_mm BETWEEN b.start_next_fom AND b.end_last_eom
GROUP BY a.date_mm
ORDER BY a.date_mm
Result:
| month | count |
|------------|-------|
| 2020-01-01 | 1 |
| 2020-02-01 | 1 |
| 2020-03-01 | 1 |
| 2020-04-01 | 2 |
| 2020-05-01 | 2 |
| 2020-06-01 | 2 |
| 2020-07-01 | 2 |
| 2020-08-01 | 2 |
| 2020-09-01 | 1 |
| 2020-10-01 | 1 |
SQL Fiddle:
http://sqlfiddle.com/#!17/9f9f3/37
One approach could be to
generate a calendar table using GENERATE_SERIES
joining the calendar table with your original table on date ranges
aggregating to count users for each month
WITH calendar AS (
SELECT DATE('2020-01-01') + (num_months::text || ' month')::interval AS months
FROM GENERATE_SERIES(0, 11) AS num_months
)
SELECT c.months, COUNT(user_id) AS cnt
FROM calendar c
INNER JOIN tab
ON c.months BETWEEN DATE_TRUNC('month', tab.start_) + INTERVAL '1 month' AND DATE_TRUNC('month', tab.end_) - INTERVAL '1 month'
GROUP BY c.months
ORDER BY c.months
Check the demo here.
If you're using a PostgreSQL legacy version, you can obtain the calendar table with a recursive query:
WITH RECURSIVE calendar AS (
SELECT '2020-01-01'::timestamp AS months,
0 AS num_months
UNION ALL
SELECT months + INTERVAL '1 month' AS months,
num_months + 1 AS num_months
FROM calendar
WHERE num_months +1 <= 12
)
SELECT c.months, COUNT(user_id) AS cnt
FROM calendar c
INNER JOIN tab
ON c.months BETWEEN DATE_TRUNC('month', tab.start_) + INTERVAL '1 month' AND DATE_TRUNC('month', tab.end_) - INTERVAL '1 month'
GROUP BY c.months
ORDER BY c.months
Check the demo here.
Example with date calendar as subquery - number sequence (10*12) - 10 year.
Test data:
create table test (user_id integer, start_date date, end_date date) ;
insert into test values
(1, 'December 22, 2019', 'June 29, 2020')
,(2, 'March 8, 2020', 'September 8, 2020')
,(3, 'May 21, 2020', 'November 21, 2020') --june-october
,(4, 'May 31, 2020', 'November 30, 2020') --june-november
,(5, 'May 01, 2020', 'May 31, 2020') -- 1 full month - may
,(6, 'May 01, 2020', 'May 30, 2020') -- part of month - null
,(7, 'May 05, 2020', 'May 30, 2020') -- part of month - null
,(8, 'May 05, 2020', 'May 31, 2020') -- part of month - null
,(9, 'May 05, 2020', 'June 6, 2020') -- part of month - null
,(10, 'May 05, 2020', 'July 6, 2020') -- part of 2 month -june
,(11, 'Jan 01, 2018', 'Dec 31, 2020') -- full 3 year
;
Main query:
SELECT user_id,start_date,end_date,mn monthNum
,to_char(case when date_trunc('month', start_date)=start_date
then (start_date+cast((mn-1) ||' month' as interval))
else (start_date+cast(mn ||' month' as interval))
end, 'YY-mm-Mon') AS month
,case when date_trunc('month', start_date)=start_date
then (start_date+cast((mn-1) ||' month' as interval))
else (start_date+cast(mn ||' month' as interval))
end date
FROM test AS t
left join --series of 120 numbers (month)
(select yn*12+mn as mn
from (select * from(values(1),(2),(3),(4),(5),(6),(7),(9),(10),(11),(12))tm(mn))tm -- month
,(select * from(values(0),(1),(2),(3),(4),(5),(6),(7),(9),(10))ty(yn)) ty --years
)mm
on case when date_trunc('month', start_date)=start_date -- first day of month
then (start_date+cast((mn-1) ||' month' as interval))
else (start_date+cast(mn ||' month' as interval))
end
<=
case when end_date
=(date_trunc('month', end_date)
+ interval '1 month' - interval '1 day') --eomonth
then end_date
else (end_date-cast(extract(day from end_date) ||' day' as interval))
end
ORDER BY user_id,mn
Somewhat complicated to check start_date is first day of month and end_date is last day of month
Fiddle here

Aggregate monthly rows created date and ended date

I need to adapt a graph from the current BI implementation to an SQL one. This graph reflects the amount of requests received and each one of these requests have 3 fields that are relevant for this query: the id, created date and the end date.
The graph looks like this https://i.stack.imgur.com/NRIjr.png:
+----+--------------+-------------+
| ID | CREATE_DATE | END_DATE |
+----+--------------+-------------+
| | | |
| 1 | 2022-01-01 | 2022-02-10 |
| | | |
| 2 | 2022-01-03 | 2022-03-01 |
| | | |
| 3 | 2022-02-01 | 2022-04-01 |
| | | |
| 4 | 2022-03-01 | null |
+----+--------------+-------------+
So for this particular example we'd have something like this:
January: active: 2 (requests 1 and 2), finished: 0;
February: active 2 (requests 2, 3), finished 1 (request 1);
March: active 2 (requests 3, 4) finished 1 (request 2)
So for each month I want the active requests for that particular month (those that their ended date goes after that particular month or is null) and the requests that finished during that month (this one might be split to another query, of course) I tried this query, but of course, it doesn't take into account the requests that ended in a particular month, and only gives me the cumulative sum
Edit: I forgot to mention that one of the requirements is that the beggining and end date of the graph might be set by the user. So maybe I want to see the months from April-2022 to April-2020 and see the 2 year behaviour!
WITH cte AS ( SELECT
date_trunc('month',
r.date_init) AS mon,
count(r.id) AS mon_sum
FROM
"FOLLOWUP"."CAT_REQUEST" r
GROUP BY
1 ) SELECT
to_char(mon,
'YYYY-mm') AS mon_text,
COALESCE(sum(c.mon_sum)
OVER (ORDER BY mon),
0) AS running_sum
FROM
generate_series('2022-01-01', '2023-12-25',
interval '1 month') mon
LEFT JOIN
cte c USING (mon)
ORDER BY
mon
I wrote query for you using some different business logic. But, result is will be same result which you needed. Sample query:
with month_list as (
select 1 as id, 'Yanuary' as mname union all
select 2 as id, 'Febriary' as mname union all
select 3 as id, 'Marth' as mname union all
select 4 as id, 'April' as mname union all
select 5 as id, 'May' as mname union all
select 6 as id, 'June' as mname union all
select 7 as id, 'Jule' as mname union all
select 8 as id, 'August' as mname union all
select 9 as id, 'September' as mname union all
select 10 as id, 'October' as mname union all
select 11 as id, 'November' as mname union all
select 12 as id, 'December' as mname
),
test_table as (
select
id,
create_date,
end_date,
extract(month from create_date) as month1,
extract(month from end_date) as month2
from
your_table
)
select
t1.mname,
count(*) as "actived"
from
month_list t1
inner join
test_table t2 on (t1.id >= t2.month1) and (t1.id < t2.month2)
group by
t1.id, t1.mname
order by
t1.id
/* --- Result:
mname actived
--------------------
Yanuary 2
Febriary 2
Marth 1
*/
PostgreSQL has many date & time functions and types.
I write some samples for you:
For example, in my samples function now() our chosen date.
-- get previos 12 month from date (return timestampt)
select now() - '12 month'::interval as newdate
-- Return:
2021-04-03 18:22:48.344 +0400
-- if you need only date, you can cast this to date
select (now() - '12 month'::interval)::date as newdate
-- Return:
2021-04-03
-- generate data from previous 12 month to selected date increase by month:
SELECT t1.datelist::date
from generate_series
(
now()-'12 month'::interval,
now(),
'1 month'
)
AS t1(datelist)
-- Return:
2021-04-03
2021-05-03
2021-06-03
2021-07-03
2021-08-03
2021-09-03
2021-10-03
2021-11-03
2021-12-03
2022-01-03
2022-02-03
2022-03-03
2022-04-03
-- generate data from previous 12 month to selected date increase by month with extracting month names and year:
-- this sample may be as you needed.
SELECT
extract(year from t1.datelist) as "year",
TO_CHAR(t1.datelist, 'Month') as "month",
trim(TO_CHAR(t1.datelist, 'Month')) || '-' || trim(to_char(t1.datelist, 'yyyy')) as "formatted_date"
from generate_series
(
now()-'12 month'::interval,
now(),
'1 month'
)
AS t1(datelist)
-- Return:
year month formatted_date
------------------------------------
2021 April April-2021
2021 May May-2021
2021 June June-2021
2021 July July-2021
2021 August August-2021
2021 September September-2021
2021 October October-2021
2021 November November-2021
2021 December December-2021
2022 January January-2022
2022 February February-2022
2022 March March-2022
2022 April April-2022

Oracle count days per month

I wrote this SQL statement to calculate the days for each month
(select count(*) DAYs FROM
(
select trunc(ADD_MONTHS(sysdate,-1),'MM') + level -1 Dates from dual connect by
level <= ADD_MONTHS(trunc(sysdate,'MM'),1)-1 - trunc(sysdate,'MM')+1
) Where To_char(dates,'DY') NOT IN ('SA','SO'))
At the moment this statement ignores Saturdays and Sundays and it calculates the days from the month before the sysdate (June).
June has 22 days without weekends but sadly my statement says it has 23. I found out it includes the 1st July, which is wrong.
Do you know how I can tell my little statement it only calculates the days from the month I want to get not including days from another month?
Doing this sort of thing is always going to look not pretty... here's one way, which does it for the entire current year. You can restrict to a single month by adding an additional statement to the where clause:
select to_char(trunc(sysdate, 'y') + level - 1, 'fmMON') as month, count(*)
from dual
where to_char(trunc(sysdate, 'y') + level - 1, 'fmDY', 'nls_date_language=english') not in ('SAT','SUN')
connect by level <= trunc(add_months(sysdate, 12), 'y') - trunc(sysdate, 'y')
group by to_char(trunc(sysdate, 'y') + level - 1, 'fmMON')
As I said, not pretty.
Note a couple of things:
Use of the fm format model modifier to remove leading spaces
Explicit use of nls_date_language to ensure it'll work in all environments
I've added 12 months to the current date and then truncated it to the first of January to get the first day of the new year for simplicity
If you want to do this by month it might be worth looking at the LAST_DAY() function
The same statement (using LAST_DAY()) for the previous month only would be:
select count(*)
from dual
where to_char(trunc(sysdate, 'y') + level - 1, 'fmDY', 'nls_date_language=english') not in ('SAT','SUN')
connect by level <= last_day(add_months(trunc(sysdate, 'mm'), -1)) - add_months(trunc(sysdate, 'mm'), -1) + 1
Firstly, your inner query (select trunc(ADD_MONTHS(sysdate,-1),'MM') + level -1 Dates from dual connect by level <= ADD_MONTHS(trunc(sysdate,'MM'),1)-1 - trunc(sysdate,'MM')+1) returns the days of the month plus one extra day from the next month.
Secondly, a simpler query could use the LAST_DAY function which gets the last day of the month.
Finally, use the 'D' date format to get the day of the week as a number.
SELECT COUNT(*) FROM (
SELECT TO_CHAR(TRUNC(SYSDATE,'MM') + ROWNUM - 1, 'D') d
FROM dual CONNECT BY LEVEL <= TO_NUMBER(TO_CHAR(LAST_DAY(SYSDATE),'DD'))
) WHERE d BETWEEN 1 AND 5;
Without having to generate all days of the month and then count them:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE FUNCTION WORK_DAYS_IN_MONTH(
dt DATE
) RETURN NUMBER DETERMINISTIC
AS
first_day DATE := TRUNC( dt, 'MM' );
remainder NUMBER := LAST_DAY( dt ) - ( first_day + INTERVAL '27' DAY );
BEGIN
RETURN 20 + CASE first_day - TRUNC( first_day, 'IW' )
WHEN 0 THEN remainder -- Monday
WHEN 1 THEN remainder -- Tuesday
WHEN 2 THEN remainder -- Wednesday
WHEN 3 THEN LEAST( remainder, 2 ) -- Thursday
WHEN 4 THEN LEAST( remainder, 1 ) -- Friday
WHEN 5 THEN GREATEST( remainder-2, 0 ) -- Saturday
ELSE GREATEST( remainder-1, 0 ) -- Sunday
END;
END;
//
Query 1:
SELECT ADD_MONTHS( DATE '2014-12-01', LEVEL ) AS "Month",
WORK_DAYS_IN_MONTH( ADD_MONTHS( DATE '2014-12-01', LEVEL ) ) AS "# Work Days"
FROM DUAL
CONNECT BY LEVEL <= 12
Results:
| Month | # Work Days |
|-----------------------------|-------------|
| January, 01 2015 00:00:00 | 22 |
| February, 01 2015 00:00:00 | 20 |
| March, 01 2015 00:00:00 | 22 |
| April, 01 2015 00:00:00 | 22 |
| May, 01 2015 00:00:00 | 21 |
| June, 01 2015 00:00:00 | 22 |
| July, 01 2015 00:00:00 | 23 |
| August, 01 2015 00:00:00 | 21 |
| September, 01 2015 00:00:00 | 22 |
| October, 01 2015 00:00:00 | 22 |
| November, 01 2015 00:00:00 | 21 |
| December, 01 2015 00:00:00 | 23 |

How to find 4th Saturday of month? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Please let me know that how to find fourth Saturday of month in oracle. suppose we put "03/02/1999" then write a sql query to find out fourth saturday of month.
In Oracle you can use this:
NEXT_DAY(TRUNC(TO_DATE('1999-02-03','yyyy-mm-dd'), 'MM')-1, 'Saturday')+3*7
TRUNC( date, [fmt] ) takes an optional second argument which determines what time portion you are truncating to; so:
TRUNC( your_date, 'mm' )
Will truncate to the first day of the month and if you subtract a day from it then you get the last day of the previous month:
TRUNC( your_date, 'mm' ) - INTERVAL '1' DAY
or, more simply,
TRUNC( your_date, 'mm' ) - 1
NEXT_DAY( date, char) will get the next day, as specified by the second argument, from the given date. Combining the two functions will give you the first specified day of the month:
NEXT_DAY( TRUNC( your_date, 'mm' ) - 1, day_of_week )
Adding two weeks to it will give you the fourth day-of-the-week of the month:
NEXT_DAY( TRUNC( your_date, 'mm' ) - 1, 'Saturday' ) + INTERVAL '21' DAY
As an example:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE dates ( your_date ) AS
SELECT TO_DATE( '2014-' || LPAD( LEVEL, 2, '0' ) || '-01', 'YYYY-MM-DD' )
FROM DUAL
CONNECT BY LEVEL < 12
/
Query 1:
SELECT NEXT_DAY( TRUNC( your_date, 'mm') - INTERVAL '1' DAY, 'Saturday') + INTERVAL '21' DAY AS fourth_saturday
FROM dates
Results:
| FOURTH_SATURDAY |
|----------------------------------|
| January, 25 2014 00:00:00+0000 |
| February, 22 2014 00:00:00+0000 |
| March, 22 2014 00:00:00+0000 |
| April, 26 2014 00:00:00+0000 |
| May, 24 2014 00:00:00+0000 |
| June, 28 2014 00:00:00+0000 |
| July, 26 2014 00:00:00+0000 |
| August, 23 2014 00:00:00+0000 |
| September, 27 2014 00:00:00+0000 |
| October, 25 2014 00:00:00+0000 |
| November, 22 2014 00:00:00+0000 |
You could also wrap the code in a (more generic) function:
CREATE FUNCTION nth_day_of_month (
month DATE,
day_of_week VARCHAR2,
nth NUMBER
) RETURN DATE DETERMINISTIC
IS
BEGIN
RETURN NEXT_DAY( TRUNC( month, 'mm' ) - INTERVAL '1' DAY, day_of_week ) + (nth - 1) * 7;
END nth_day_of_month;
/
Query 2:
SELECT TO_CHAR( nth_day_of_month( your_date, 'Monday', 1 ), 'DAY YYYY-MM-DD' ) AS first_monday,
TO_CHAR( nth_day_of_month( your_date, 'Saturday', 4 ), 'DAY YYYY-MM-DD' ) AS fourth_saturday
FROM dates
Results:
| FIRST_MONDAY | FOURTH_SATURDAY |
|-------------------|---------------------|
| MONDAY 2014-01-06 | SATURDAY 2014-01-25 |
| MONDAY 2014-02-03 | SATURDAY 2014-02-22 |
| MONDAY 2014-03-03 | SATURDAY 2014-03-22 |
| MONDAY 2014-04-07 | SATURDAY 2014-04-26 |
| MONDAY 2014-05-05 | SATURDAY 2014-05-24 |
| MONDAY 2014-06-02 | SATURDAY 2014-06-28 |
| MONDAY 2014-07-07 | SATURDAY 2014-07-26 |
| MONDAY 2014-08-04 | SATURDAY 2014-08-23 |
| MONDAY 2014-09-01 | SATURDAY 2014-09-27 |
| MONDAY 2014-10-06 | SATURDAY 2014-10-25 |
| MONDAY 2014-11-03 | SATURDAY 2014-11-22 |
select next_day(trunc(to_date('1999-02- 03','yyyy-mm-dd'),'mm')-1,'saturday')+21
from dual;
Try this
DECLARE #StartDate DATETIME;
SELECT #StartDate = DATEADD(year, DATEDIFF(year,0,GETDATE()), 0);
-- This clause is just to get some numbers: a Tally table, for joining to get a range of dates
WITH NumDays AS (
SELECT TOP 365 -- one year of days
ROW_NUMBER() OVER (ORDER BY sc1.ID) AS N
FROM Master.dbo.SysColumns sc1,
Master.dbo.SysColumns sc2
UNION SELECT 0 -- just to get a zero in the results
), DateRange AS (
SELECT N,
DATEADD(day, N, #StartDate) AS [Date],
DATEPART(dw,DATEADD(day, N, #StartDate)) AS [Day],
DATENAME(dw,DATEADD(day, N, #StartDate)) AS [DayName],
DATENAME(week,DATEADD(day, N, #StartDate)) AS [WeekNo]
FROM NumDays
)
SELECT *
FROM DateRange
WHERE [DayName] = 'Saturday'
AND WeekNo in (4)
Here you can give weekNo comma separated. Suppose you want find 2nd and 4th Saturday then add this WeekNo in (2,4) instead of WeekNo in (4)

Get the difference between two dates both In Months and days in sql

I need to get the difference between two dates say if the difference is 84 days, I should probably have output as 2 months and 14 days, the code I have just gives the totals. Here is the code
SELECT Months_between(To_date('20120325', 'YYYYMMDD'),
To_date('20120101', 'YYYYMMDD'))
num_months,
( To_date('20120325', 'YYYYMMDD') - To_date('20120101', 'YYYYMMDD') )
diff_in_days
FROM dual;
Output is:
NUM_MONTHS DIFF_IN_DAYS
2.774193548 84
I need for example the output for this query to be either 2 months and 14 days at worst, otherwise I won't mind if I can have the exact days after the months figure because those days are not really 14 because all months do not have 30 days.
select
dt1, dt2,
trunc( months_between(dt2,dt1) ) mths,
dt2 - add_months( dt1, trunc(months_between(dt2,dt1)) ) days
from
(
select date '2012-01-01' dt1, date '2012-03-25' dt2 from dual union all
select date '2012-01-01' dt1, date '2013-01-01' dt2 from dual union all
select date '2012-01-01' dt1, date '2012-01-01' dt2 from dual union all
select date '2012-02-28' dt1, date '2012-03-01' dt2 from dual union all
select date '2013-02-28' dt1, date '2013-03-01' dt2 from dual union all
select date '2013-02-28' dt1, date '2013-04-01' dt2 from dual union all
select trunc(sysdate-1) dt1, sysdate from dual
) sample_data
Results:
| DT1 | DT2 | MTHS | DAYS |
----------------------------------------------------------------------------
| January, 01 2012 00:00:00 | March, 25 2012 00:00:00 | 2 | 24 |
| January, 01 2012 00:00:00 | January, 01 2013 00:00:00 | 12 | 0 |
| January, 01 2012 00:00:00 | January, 01 2012 00:00:00 | 0 | 0 |
| February, 28 2012 00:00:00 | March, 01 2012 00:00:00 | 0 | 2 |
| February, 28 2013 00:00:00 | March, 01 2013 00:00:00 | 0 | 1 |
| February, 28 2013 00:00:00 | April, 01 2013 00:00:00 | 1 | 1 |
| August, 14 2013 00:00:00 | August, 15 2013 05:47:26 | 0 | 1.241273 |
Link to test: SQLFiddle
Updated for correctness. Originally answered by #jen.
with DATES as (
select TO_DATE('20120101', 'YYYYMMDD') as Date1,
TO_DATE('20120325', 'YYYYMMDD') as Date2
from DUAL union all
select TO_DATE('20120101', 'YYYYMMDD') as Date1,
TO_DATE('20130101', 'YYYYMMDD') as Date2
from DUAL union all
select TO_DATE('20120101', 'YYYYMMDD') as Date1,
TO_DATE('20120101', 'YYYYMMDD') as Date2
from DUAL union all
select TO_DATE('20130228', 'YYYYMMDD') as Date1,
TO_DATE('20130301', 'YYYYMMDD') as Date2
from DUAL union all
select TO_DATE('20130228', 'YYYYMMDD') as Date1,
TO_DATE('20130401', 'YYYYMMDD') as Date2
from DUAL
), MONTHS_BTW as (
select Date1, Date2,
MONTHS_BETWEEN(Date2, Date1) as NumOfMonths
from DATES
)
select TO_CHAR(Date1, 'MON DD YYYY') as Date_1,
TO_CHAR(Date2, 'MON DD YYYY') as Date_2,
NumOfMonths as Num_Of_Months,
TRUNC(NumOfMonths) as "Month(s)",
ADD_MONTHS(Date2, - TRUNC(NumOfMonths)) - Date1 as "Day(s)"
from MONTHS_BTW;
SQLFiddle Demo :
+--------------+--------------+-----------------+-----------+--------+
| DATE_1 | DATE_2 | NUM_OF_MONTHS | MONTH(S) | DAY(S) |
+--------------+--------------+-----------------+-----------+--------+
| JAN 01 2012 | MAR 25 2012 | 2.774193548387 | 2 | 24 |
| JAN 01 2012 | JAN 01 2013 | 12 | 12 | 0 |
| JAN 01 2012 | JAN 01 2012 | 0 | 0 | 0 |
| FEB 28 2013 | MAR 01 2013 | 0.129032258065 | 0 | 1 |
| FEB 28 2013 | APR 01 2013 | 1.129032258065 | 1 | 1 |
+--------------+--------------+-----------------+-----------+--------+
Notice, how for the last two dates, Oracle reports the decimal part of months (which gives days) incorrectly. 0.1290 corresponds to exactly 4 days with Oracle considering 31 days in a month (for both March and April).
I think that your question is not defined well enough, for the following reason.
Answers relying on months_between have to deal with the following issue: that the function reports exactly one month between 2013-02-28 and 2013-03-31, and between 2013-01-28 and 2013-02-28, and between 2013-01-31 and 2013-02-28 (I suspect that some answerers have not used these functions in practice, or are now going to have to review some production code!)
This is documented behaviour, in which dates that are both the last in their respective months or which fall on the same day of the month are judged to be an integer number of months apart.
So, you get the same result of "1" when comparing 2013-02-28 with 2013-01-28 or with 2013-01-31, but comparing it with 2013-01-29 or 2013-01-30 gives 0.967741935484 and 0.935483870968 respectively -- so as one date approaches the other the difference reported by this function can increase.
If this is not an acceptable situation then you'll have to write a more complex function, or just rely on a calculation that assumes 30 (for example) days per month. In the latter case, how will you deal with 2013-02-28 and 2013-03-31?
is this what you've ment ?
select trunc(months_between(To_date('20120325', 'YYYYMMDD'),to_date('20120101','YYYYMMDD'))) months,
round(To_date('20120325', 'YYYYMMDD')-add_months(to_date('20120101','YYYYMMDD'),
trunc(months_between(To_date('20120325', 'YYYYMMDD'),to_date('20120101','YYYYMMDD'))))) days
from dual;
Here I'm just doing the difference between today, and a CREATED_DATE DATE field in a table, which obviously is a date in the past:
SELECT
((FLOOR(ABS(MONTHS_BETWEEN(CREATED_DATE, SYSDATE))) / 12) * 12) || ' months, ' AS MONTHS,
-- we take total days - years(as days) - months(as days) to get remaining days
FLOOR((SYSDATE - CREATED_DATE) - -- total days
(FLOOR((SYSDATE - CREATED_DATE)/365)*12)*(365/12) - -- years, as days
-- this is total months - years (as months), to get number of months,
-- then multiplied by 30.416667 to get months as days (and remove it from total days)
FLOOR(((SYSDATE - CREATED_DATE)/365)*12 - (FLOOR((SYSDATE - CREATED_DATE)/365)*12)) * (365/12))
|| ' days ' AS DAYS
FROM MyTable
I use (365/12), or 30.416667, as my conversion factor because I'm using total days and removing years and months (as days) to get the remainder number of days. It was good enough for my purposes, anyway.
The solution I post will consider a month with 30 days
select CONCAT (CONCAT (num_months,' MONTHS '), CONCAT ((days-(num_months)*30),' DAYS '))
from (
SELECT floor(Months_between(To_date('20120325', 'YYYYMMDD'),
To_date('20120101', 'YYYYMMDD')))
num_months,
( To_date('20120325', 'YYYYMMDD') - To_date('20120101', 'YYYYMMDD') )
days
FROM dual);
Find out Year - Month- Day between two Days in Orale Sql
select
trunc(trunc(months_between(To_date('20120101', 'YYYYMMDD'),to_date('19910228','YYYYMMDD')))/12) years ,
trunc(months_between(To_date('20120101', 'YYYYMMDD'),to_date('19910228','YYYYMMDD')))
-
(trunc(trunc(months_between(To_date('20120101', 'YYYYMMDD'),to_date('19910228','YYYYMMDD')))/12))*12
months,
round(To_date('20120101', 'YYYYMMDD')-add_months(to_date('19910228','YYYYMMDD'),
trunc(months_between(To_date('20120101', 'YYYYMMDD'),to_date('19910228','YYYYMMDD'))))) days
from dual;
SELECT (MONTHS_BETWEEN(date2,date1) + (datediff(day,date2,date1))/30) as num_months,
datediff(day,date2,date1) as diff_in_days FROM dual;
// You should replace date2 with TO_DATE('2012/03/25', 'YYYY/MM/DD')
// You should replace date1 with TO_DATE('2012/01/01', 'YYYY/MM/DD')
// To get you results
See the query below (assumed #dt1 >= #dt2);
Declare #dt1 datetime = '2013-7-3'
Declare #dt2 datetime = '2013-5-2'
select abs(DATEDIFF(DD, #dt2, #dt1)) Days,
case when #dt1 >= #dt2
then case when DAY(#dt2)<=DAY(#dt1)
then Convert(varchar, DATEDIFF(MONTH, #dt2, #dt1)) + CONVERT(varchar, ' Month(s) ') + Convert(varchar, DAY(#dt1)-DAY(#dt2)) + CONVERT(varchar, 'Day(s).')
else Convert(varchar, DATEDIFF(MONTH, #dt2, #dt1)-1) + CONVERT(varchar, ' Month(s) ') + convert(varchar, abs(DATEDIFF(DD, #dt1, DateAdd(Month, -1, #dt1))) - (DAY(#dt2)-DAY(#dt1))) + CONVERT(varchar, 'Day(s).')
end
else 'See asumption: #dt1 must be >= #dt2'
end In_Months_Days
Returns:
Days | In_Months_Days
62 | 2 Month(s) 1Day(s).