Number of days in a month
Asked Answered
A

7

11

I have a monthly amount that I need to spread equally over the number of days in the month. The data looks like this:

Month       Value
----------- ---------------
01-Jan-2012 100000
01-Feb-2012 121002
01-Mar-2012 123123
01-Apr-2012 118239

I have to spread the Jan amount over 31 days, the Feb amount over 29 days and the March amount over 31 days.

How can I use PL/SQL to find out how many days there are in the month given in the month column?

Amata answered 7/12, 2012 at 16:1 Comment(0)
H
10

Don't use to_char() and stuff when doing arithmetics with dates. Strings are strings and dates are dates. Please respect the data types and use this instead:

1+trunc(last_day(date_column))-trunc(date_column,'MM')

Indeed, this is correct. It computes the difference between the value of the last day of the month and the value of the first (which is obviously always 1 and therefore we need to add this 1 again).

You must not forget to use the trunc() function if your date columns contains time, because last_day() preserves the time component.

Harmless answered 27/10, 2016 at 11:34 Comment(0)
G
20
SELECT CAST(to_char(LAST_DAY(date_column),'dd') AS INT)
  FROM table1
Guardrail answered 7/12, 2012 at 16:8 Comment(1)
Wow, this is what I call "pythonic" :)Uproarious
H
10

Don't use to_char() and stuff when doing arithmetics with dates. Strings are strings and dates are dates. Please respect the data types and use this instead:

1+trunc(last_day(date_column))-trunc(date_column,'MM')

Indeed, this is correct. It computes the difference between the value of the last day of the month and the value of the first (which is obviously always 1 and therefore we need to add this 1 again).

You must not forget to use the trunc() function if your date columns contains time, because last_day() preserves the time component.

Harmless answered 27/10, 2016 at 11:34 Comment(0)
P
8
SELECT EXTRACT(DAY FROM LAST_DAY(SYSDATE)) num_of_days FROM dual;
/
SELECT SYSDATE, TO_CHAR(LAST_DAY(SYSDATE), 'DD') num_of_days FROM dual
/
-- Days left in a month --
SELECT SYSDATE, LAST_DAY(SYSDATE) "Last", LAST_DAY(SYSDATE) - SYSDATE "Days left"
FROM DUAL
/
Pearliepearline answered 7/12, 2012 at 22:39 Comment(0)
D
5

You can add a month and subtract the two dates

SQL> ed
Wrote file afiedt.buf

  1  with x as (
  2      select date '2012-01-01' dt from dual union all
  3      select date '2012-02-01' from dual union all
  4      select date '2012-03-01' from dual union all
  5      select date '2012-01-31' from dual
  6    )
  7    select dt, add_months(trunc(dt,'MM'),1) - trunc(dt,'MM')
  8*     from x
SQL> /

DT        ADD_MONTHS(TRUNC(DT,'MM'),1)-TRUNC(DT,'MM')
--------- -------------------------------------------
01-JAN-12                                          31
01-FEB-12                                          29
01-MAR-12                                          31
31-JAN-12                                          31
Danicadanice answered 7/12, 2012 at 16:3 Comment(4)
try the same thing using 2012-01-31. You get 29 instead of 31, which is incorrect.Amata
@RajMore - I'm not sure that I understand your point. If you add 1 month to 2012-01-31, that gives you 2012-02-29. Subtracting the two gives 29 which is correct. Of course, that gives you the number of days in February, not January. The sample data you posted were all from the first of the month, though, not the last day of the month. You can trunc(some_date, 'MM') to convert the date to the first of the month in order to match the sample data that you posted.Danicadanice
I just put together sample data for the sake of an example - the data is actually spread all over the month.Amata
@RajMore - When you're putting together sample data, please try to cover as much as possible the actual use cases that you need to handle. If your sample data is all on the first and you don't specify that the real data occurs throughout the month, folks will naturally assume that you are just storing the month in your data. I updated my answer to include the trunc.Danicadanice
D
1
select add_months(my_date, 1)-my_date from dual;
Dimarco answered 7/12, 2012 at 16:8 Comment(2)
This is wrong. Consider 2016-10-31 as input. It returns 30 days.Harmless
The question clearly shows that the date will always be the first of the month, so this is a valid answer. If you have a different requirement you can do select add_months(TRUNC(my_date,'MM'), 1)-TRUNC(my_date,'MM') from dual;Brownnose
N
0
SELECT TO_CHAR(LAST_DAY(SYSDATE), 'fmday-mon-rr dd') as LastDayOfMonth
FROM dual;
Nobles answered 9/4, 2015 at 9:59 Comment(0)
C
0

Use the following Oracle query:

select to_number(to_char(last_day(sysdate),'dd')) TotalDays from dual

Date_Parameter='01-Oct-2017'
select to_number(to_char(last_day('Date_Parameter'),'dd')) TotalDays from dual
Camera answered 11/10, 2017 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.