Day of Month must be between 1 and last day of month - oracle

Trying to write a query where I can see the format of a date using the query below
select to_date((substr(Key ,8,15)),'yyyymmdd') from Myrules
where LENGTH(substr(Key ,8,15)) = 8;
the data looks like this in the Key field
AA-FFL-20200706
AA-FFL-20200961
AZ-MDL-20200961
AZ-MGL-4
I'm getting the error saying "day of month must be between 1 and last day of month" Any idea what am I doing wrong above?

There's no day 61 in any month I know.
AA-FFL-20200961
AZ-MDL-20200961

Related

How can I get the last day of the month as my end date on a BIRT report?

I am having issues getting the last day of month on my report.
I used the add month function to add a moth but it kept on giving me the first day of the next month. I only want the last day of the month as my end date and not the first day of the next month.
To get the last day of the current month enter the following Javascript:
now = new Date();
new Date (new Date(now.getFullYear(), now.getMonth() +1 , 1) - 1);

Single Month Digit Date Format issue in Oracle

Am getting the below issue when am using 'mon-d-yyyy' to convert date to char, as i need a single day digit for values from 1 to 9 days in a month.
When i use the 'mon-d-yyyy' format, am losing out on 5 days and getting a wrong date. Any help on this would be great.
select to_char(sysdate-22,'mon-d-yyyy') from dual;--aug-2-2017
select to_char(sysdate-22,'mon-dd-yyyy') from dual;--aug-07-2017
select sysdate-22 from dual;--07-AUG-17 11.06.43
In Oracle date formats, d gets the day of week. The 2 in your output means monday, not august the 2nd.
Try using Fill Mode as Format Model Modifier
select to_char(sysdate-22,'mon-fmdd-yyyy') from dual;
One option might be to piece together the date output you want:
SELECT
TO_CHAR(sysdate-22, 'mon-') ||
TRIM(LEADING '0' FROM TO_CHAR(sysdate-22, 'dd-')) ||
TO_CHAR(sysdate-22, 'yyyy')
FROM dual;
The middle term involving TRIM strips off the leading zeroes, if present, from the date.
Output:
Demo here:
Rextester
SQL>SELECT TO_CHAR(TO_DATE('29-AUG-2017','DD-MON-YYYY') - 22,'"WEEKDAY :"D, MON-FMDD-YYYY') "Before22Days" FROM DUAL;
D- Gives you a numeric weekday(2nd weekday in a week) on AUG-07-2017.
DD-Gives a Numeric Month Day i.e,07th
FMDD-Gives 7th
Before22Days
----------------------
WEEKDAY :2, AUG-7-2017

PL/SQL weekly Aggregation Logic with dynamic time range

I need to aggregate the values at weekly interval. My date range is dynamic means i can give any start date and end date. Every sunday should be the starting week of every month. say if i have two columns and my start and end date is 07/11/2016 to 13/11/2016
column A column B
07/11/2016 23
08/11/2016 20
09/11/2016 10
10/11/2016 05
11/11/2016 10
12/11/2016 20
13/11/2016 10
My result should come like taking the average of column B
Column A Column B
13/11/2016 14.00
It means i should consider the past value and aggregate it to the day Sunday of that week. Also if my start and end date is like 07/11/2016 to 10/11/2016 then I should not aggregate the value as my week is not complete. I am able to aggregate the values but if my week is not complete i m not able to restrict the aggregation.
Is there any way to do this in PL/SQL??
Thank you in advance.
select to_char(columnA, 'iw') as weeknumber, avg(columnB)
from table
group by to_char(columnA, 'iw');
This will aggregate by number of week. If you need to show last day of week as a label you can get it as max(columnA) over (partition by to_char(columnA, 'iw'))

breakdown monthly report to weekly

I was trying to generate a monthly report but I have no idea how to break it down in weeks. e.g when i generate January my output report should be divided in 4 week - First week/Second week/Third week/Fourth week OF JANUARY - is this even possible? Should it be done before saving to database or SQL will do? I have a datetime field called RecordDate
I am using SQL Server 2005,VS 2010 and CR for VS2010.
Given your datetime field RecordDate, the following SQL will give the week of the month (starting at 1)
select (((datepart(d, RecordDate)-1) / 7)+1)
if you group by that, you should be able to produce a breakdown by the weeks in a month.
Of course, doing it this way some of the 'weeks' will not be 7 days long. It may be that you really want to group by the week of the year, ie.
select datepart(wk, RecordDate)
In each case you will need to produce labels. If you're going to do this in SQL then in the first case to get a label like 'week 1 of month 7' it'll be something like
select 'week ' + cast( (((datepart(d, RecordDate)-1) / 7)+1) as char(1))
+ ' of month ' + cast(datepart(month, RecordDate) as varchar(2))
from Table
group by (((datepart(d, RecordDate)-1) / 7)+1), datepart(month, RecordDate)
You'd have to go round the houses a bit to get a label like 'week 1 starting in month 7' for the second case (I will leave this as an exercise for the reader)

How to get date from month as input in oracle?

My query is , I need to give input a month ,i.e ex :input will be 'MAR' then i need to get 1st date and last date for the March month .
select to_char(trunc(trunc(sysdate, 'MM'), 'MM'),'DD-MON-YYYY') "First Day of Last Month"
, to_char(trunc(sysdate, 'MM') ,'DD-MON-YYYY') "Last Day of Last Month"
from dual ;
Any one please help..
month_start := to_date('MAR', 'MON');
month_end := last_day(month_start);
Month names depend on specifics of the NLS language/territory so may need to tweak them. (alter session set nls_language/nls_territory = AMERICAN/AMERICA).
You haven't mentioned what would be the year, so the assumption is that you need current one.
If the year is also a variable input:
month_start := to_date('2015-MAR', 'yyyy-MON');

Resources