Concatenated Select List value returns 'not a valid month' when applied TO_DATE - oracle

I have month and year values from select list, concatenated them in a string. If TO_DATE is applied on the string, that returns 'not a valid month' error.
Below is my code snippet,
V_DATE_FROM_STR_TEST := TRIM('01-'||TRIM(TO_CHAR(:P3_FROM_MONTH))||'-'||TRIM(TO_CHAR(:P3_YEAR)));
V_DATE_TO_STR_TEST := TRIM('01-'||TRIM(TO_CHAR(:P3_TO_MONTH))||'-'||TRIM(TO_CHAR(:P3_YEAR)));
Error 'not a valid month' occurs on this line,
TRUNC(TO_DATE(V_DATE_FROM_STR_TEST, 'DD-MON-YYYY'))
I have tried printing by HTP.PRN(V_DATE_FROM_STR_TEST) that shows date 01-JAN-2020.
Adding LOVs
Month LOV
select to_char(add_months(trunc(sysdate, 'yyyy'), level - 1), 'MONTH') d,
to_char(add_months(trunc(sysdate, 'yyyy'), level - 1), 'MON') r
from dual
connect by level <= 12
Year LOV
select YEAR d, YEAR r
from (select to_number(to_char(sysdate,'yyyy')) -8 YEAR
from dual
UNION
select to_number(to_char(sysdate,'yyyy')) -7 YEAR
from dual
UNION
select to_number(to_char(sysdate,'yyyy')) -6 YEAR
from dual
UNION
select to_number(to_char(sysdate,'yyyy')) -5 YEAR
from dual
UNION
select to_number(to_char(sysdate,'yyyy')) -4 YEAR
from dual
UNION
select to_number(to_char(sysdate,'yyyy')) -3 YEAR
from dual
UNION
select to_number(to_char(sysdate,'yyyy')) -2 YEAR
from dual
UNION
select to_number(to_char(sysdate,'yyyy')) -1 YEAR
from dual
UNION
select to_number(to_char(sysdate,'yyyy')) YEAR
from dual
) order by year desc
I am guessing there are some unwanted character is added in the date string from select list value, how can I remove that?

If you switch to digits, things might improve.
Month LoV: display words, but return numbers:
select to_char(add_months(trunc(sysdate, 'yyyy'), level - 1), 'MONTH') d,
level r
from dual
connect by level <= 12;
Then use this for the resulting string:
to_date(lpad(:P3_FROM_MONTH, 2, '0') || :P3_YEAR, 'mmyyyy')
Also, all those TRIMs look unnecessary, but OK - I understand - you tried to fix the problem.

While not directly referenced you can simplify your "Year LOV" query.
select yr d, yr r
from ( select extract(year from sysdate)-level +1 yr
from dual
connect by level <= 9
)
order by yr desc;

Related

how to get last businessday of last month in oralce

I have data like this my table
2020-01-01 H
2020-01-02 B
2020-01-03 B
2020-01-04 B
.
2020-01-29 B
2020-01-30 H
2020-01-31 H
2020-01-02 H
2020-02-02 H
2020-02-03 B
2020-02-04 B
2020-02-05 B
.
now my problem is in the current month i need to check third business day i.e in this case 2020-02-05 i need to get last business day of last month. i.e.2020-01-29
By adding 2 columns:
row_number() over(partition by trunc(date_value,'MM'), day_type order by date_value) as rn_month_asc,
row_number() over(partition by trunc(date_value,'MM'), day_type order by date_value desc) as rn_month_desc
in a month the 3rd business day will have rn_month_asc=3 and day_type ='B' and the latest business day will have rn_month_desc=1 and day_type ='B', and easy to query other situations if you need to.
in the current month I need to check third business day
From Oracle 12, you can use:
SELECT date_value
FROM table_name
WHERE TRUNC(SYSDATE, 'MM') <= date_value
AND date_value < ADD_MONTHS(TRUNC(SYSDATE, 'MM'), 1)
AND day_type = 'B'
ORDER BY date_value ASC
OFFSET 2 ROWS
FETCH NEXT ROW ONLY;
Which, for the sample data:
CREATE TABLE table_name (date_value, day_type) AS
SELECT DATE '2020-01-01', 'H' FROM DUAL UNION ALL
SELECT DATE '2020-01-02', 'B' FROM DUAL UNION ALL
SELECT DATE '2020-01-03', 'B' FROM DUAL UNION ALL
SELECT DATE '2020-01-04', 'B' FROM DUAL UNION ALL
SELECT DATE '2020-01-05', 'B' FROM DUAL UNION ALL
SELECT DATE '2020-01-28', 'B' FROM DUAL UNION ALL
SELECT DATE '2020-01-29', 'B' FROM DUAL UNION ALL
SELECT DATE '2020-01-30', 'H' FROM DUAL UNION ALL
SELECT DATE '2020-01-31', 'H' FROM DUAL UNION ALL
SELECT DATE '2020-01-02', 'H' FROM DUAL UNION ALL
SELECT DATE '2020-02-02', 'H' FROM DUAL UNION ALL
SELECT DATE '2020-02-03', 'B' FROM DUAL UNION ALL
SELECT DATE '2020-02-04', 'B' FROM DUAL UNION ALL
SELECT DATE '2020-02-05', 'B' FROM DUAL;
If the current month was 2020-01 then the output is:
DATE_VALUE
04-JAN-20
I need to get last business day of last month
SELECT date_value
FROM table_name
WHERE ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -1) <= date_value
AND date_value < TRUNC(SYSDATE, 'MM')
AND day_type = 'B'
ORDER BY date_value DESC
FETCH FIRST ROW ONLY;
If the current month is 2020-02 then the output is:
DATE_VALUE
29-JAN-20
fiddle

How to convert this code from oracle to redshift?

I am trying to implement the same in redshift and i am finding it little difficult to do that. Since redshift is in top of postgresql engine, if any one can do it in postgresql it would be really helpfull. Basically the code gets the count for previous two month at column level. If there is no count for exact previous month then it gives 0.
This is my code:
with abc(dateval,cnt) as(
select 201908, 100 from dual union
select 201907, 200 from dual union
select 201906, 300 from dual union
select 201904, 600 from dual)
select dateval, cnt,
last_value(cnt) over (order by dateval
range between interval '1' month preceding
and interval '1' month preceding ) m1,
last_value(cnt) over (order by dateval
range between interval '2' month preceding
and interval '2' month preceding ) m2
from (select to_date(dateval, 'yyyymm') dateval, cnt from abc)
I get error in over by clause. I tried to give cast('1 month' as interval) but still its failing. Can someone please help me with this windows function.
expected output:
Regards
This is how I would do it. In Redshift there's no easy way to generate sequences, do I select row_number() from an arbitrary table to create a sequence:
with abc(dateval,cnt) as(
select 201908, 100 union
select 201907, 200 union
select 201906, 300 union
select 201904, 600),
cal(date) as (
select
add_months(
'20190101'::date,
row_number() over () - 1
) as date
from <an arbitrary table to generate a sequence of rows> limit 10
),
with_lag as (
select
dateval,
cnt,
lag(cnt, 1) over (order by date) as m1,
lag(cnt, 2) over (order by date) as m2
from abc right join cal on to_date(dateval, 'YYYYMM') = date
)
select * from with_lag
where dateval is not null
order by dateval

Subqueries in 2 tables

I have 2 tables: machine and work.
Table:machine
machine_no downtime location
A1-100-01 2 A1
A1-100 1.5 A1
A1-200 3 A1
CC3-100-01 0.5 CC3
CC3-100 1.5 CC3
Table:work
machine_no date
A1-100-01 2/4/14
A1-100 2/14/14
A1-200 2/6/14
CC3-100-01 3/15/14
CC3-100 3/2/14
I want the output to be like this:
machine_no total_downtime month
A1-100 3.5 (total of A1-100, A1-100-01) 02
A1-200 3 02
When location A1 is selected.
SELECT machine_no, SUM(downtime) as total_downtime
FROM (
SELECT
SUBSTR(machine_no, 1,
CASE WHEN INSTR(machine_no, '-', 1, 2) = 0
THEN LENGTH(machine_no)
ELSE INSTR(machine_no, '-', 1, 2)-1
END) as machine_no,
downtime
FROM machine
WHERE location='A1'
) InnerQuery
GROUP BY machine_no
How do I join table WORK and display the month? I'm using Oracle.
Thank you.
The month column's semantics in your expected query result is unclear. Assuming that it is another aggregation "key", then your query would be
select
regexp_substr(M.machine_no, '^[^-]+-[^-]+') as machine_no,
sum(downtime) as total_downtime,
to_char(W.date, 'mm') as month
from machine M
join work W
on W.machine_no = M.machine_no
group by
regexp_substr(M.machine_no, '^[^-]+-[^-]+'),
to_char(W.date, 'mm')
;
Assuming it is a (somehow) aggregated value, let's say via min() function, then your query would be
select
regexp_substr(M.machine_no, '^[^-]+-[^-]+') as machine_no,
sum(downtime) as total_downtime,
min(to_char(W.date, 'mm')) as month
from machine M
join work W
on W.machine_no = M.machine_no
group by
regexp_substr(M.machine_no, '^[^-]+-[^-]+')
;
Both of these, in addition, assume that the (total of A1-100, A1-100-01) in your expected result is just your note, not really a part of the result. But if not, then your query could be something along the lines of
select
regexp_substr(M.machine_no, '^[^-]+-[^-]+') as machine_no,
sum(downtime)||
case when count(1) > 1 then
' (total of '||
listagg(M.machine_no)
within group (order by M.machine_no)||
')'
end
as total_downtime,
to_char(W.date, 'mm') as month
from machine M
join work W
on W.machine_no = M.machine_no
group by
regexp_substr(M.machine_no, '^[^-]+-[^-]+'),
to_char(W.date, 'mm')
;
And even this works because of a few more assumptions about the (unsaid) properties of your machine and work tables, so I'm going to stop my answer here. :-)
User regular expression to take sub string of machine_no and to_char to get the month
WITH machine(machine_no, downtime, location) as (
select 'A1-100-01', 2, 'A1' from dual union all
select 'A1-100', 1.5, 'A1' from dual union all
select 'A1-200', 3, 'A1' from dual union all
select 'CC3-100-01', 0.5, 'CC3' from dual union all
select 'CC3-100', 1.5, 'CC3' from dual),
work(machine_no, ddate) as (
select 'A1-100-01', to_date('2/4/14', 'mm/dd/yyyy') from dual union all
select 'A1-100', to_date('2/14/14', 'mm/dd/yyyy') from dual union all
select 'A1-200', to_date('2/6/14', 'mm/dd/yyyy') from dual union all
select 'CC3-100-01', to_date('3/15/14', 'mm/dd/yyyy') from dual union all
select 'CC3-100', to_date('3/2/14', 'mm/dd/yyyy') from dual)
--End of data preparation
SELECT regexp_substr(m.machine_no, '^\w+-\w+') AS machine_no,
sum(m.downtime) downtime_sum,
to_char(w.ddate , 'MM') MONTH
FROM WORK w
JOIN machine m ON m.machine_no = w.machine_no
WHERE m.location = 'A1'
GROUP BY regexp_substr(m.machine_no, '^\w+-\w+'),
to_char(w.ddate , 'MM');
Output:
| MACHINE_NO | DOWNTIME_SUM | MONTH |
|------------|--------------|-------|
| A1-200 | 3 | 02 |
| A1-100 | 3.5 | 02 |

How to display the days in 1 week in oracle?

I'm stuck, please help me how to display the cover days (e.g Monday, Tuesday, etc) per week. Please help me. Thank you!
Desired Sample Output:
Data,03/24/2014,Monday,20,Tuesday,30.....
Data,03/31/2014,Monday,12,Tuesday,20.....
Here's my script:
select 'Data'
||','||to_char(d.dtime_day, 'MM/dd/yyyy')
||','||nvl(g.total, 0)
from tablename d
left join (
select trunc(t.create_time, 'IW') as ddate
,count(t.create_time) as total
from tablename1 t
left join tablename2 q
on q.id = t.queue_id
where t.create_time between trunc(sysdate,'IW')-12*7 and sysdate -1
and q.name not like 'Data1%'
or q.name not like 'Data2%'
or q.name not like 'Data3%'
or q.name not like 'Data4%'
or q.name not like 'Data5%'
or q.name not like 'Data6%'
or q.name not like 'Data7%'
or q.name not like 'Data8%'
or q.name not like 'Data9%'
group by trunc(t.create_time, 'IW')
) g on d.dtime_day = g.ddate
where d.dtime_day between trunc(sysdate,'IW')-12*7 and trunc(sysdate) -1
and trunc(d.dtime_day, 'IW')= d.dtime_day
order by d.dtime_day;
Output of the script:
Data,03/24/2014,42
Data,03/31/2014,25
You can also select the day of the week using to_char e.g.
select to_char(sysdate, 'Day') from dual;
Used in Oracle Apex for a LOV in a Form
select basic_day display, basic_day return
from
(select trim(to_char(basic_date, 'DAY')) basic_day
from
( select 7 my_row, trunc(sysdate, 'DAY') basic_date
from dual
union
select 1, trunc(sysdate, 'DAY') + 1
from dual
union
select 2, trunc(sysdate, 'DAY') + 2
from dual
union
select 3, trunc(sysdate, 'DAY') + 3
from dual
union
select 4, trunc(sysdate, 'DAY') + 4
from dual
union
select 5, trunc(sysdate, 'DAY') + 5
from dual
union
select 6, trunc(sysdate, 'DAY') + 6
from dual)
order by my_row
)

how to picking up the min date only one line in the table by Group by

WITH A AS (
SELECT SYSDATE date_1,'1' data_1 ,1 item FROM DUAL
UNION
SELECT add_months(SYSDATE,7) date_1,'8'data, '1' item_ FROM DUAL
UNION
SELECT add_months(SYSDATE,6)+ 10, '4' data, '1' item FROM dual
UNION
SELECT add_months(SYSDATE,6) date_1,'3' data, '2' item FROM DUAL
)
SELECT min(date_1), data,item
FROM a
WHERE date_1 BETWEEN add_months(SYSDATE,6) AND last_day(add_months(SYSDATE,7))
GROUP BY item,data
the result show that item 1 has two lines, but I only want the min date one, how to change the code to display one data for one item at min(date).
*and i need the date match the right data and item *.
thank you for reading and look forward for right answer.
i find a not good way to invoke what i mean: group by item and select the min date_1 in the scope
WITH A AS (
SELECT SYSDATE date_1, '1' data, '1' item FROM DUAL
UNION SELECT add_months(SYSDATE,7) date_1, '8' data, '1' item FROM DUAL
UNION SELECT add_months(SYSDATE,6)+ 10, '4' data, '1' item FROM dual
UNION SELECT add_months(SYSDATE,6) date_1, '3' data, '2' item FROM DUAL
)
select a.* from A a,(select min(date_1) date_1,item from A
where
date_1 BETWEEN add_months(SYSDATE, 6)
AND last_day(add_months(SYSDATE, 7))
group by item) b where a.date_1 = b.date_1 and a.item= b.item
Use KEEP ... FIRST:
SQL> WITH A AS (
2 SELECT SYSDATE date_1, '1' data, '1' item FROM DUAL
3 UNION SELECT add_months(SYSDATE,7) date_1, '8' data, '1' item FROM DUAL
4 UNION SELECT add_months(SYSDATE,6)+ 10, '4' data, '1' item FROM dual
5 UNION SELECT add_months(SYSDATE,6) date_1, '3' data, '2' item FROM DUAL
6 )
7 SELECT MIN(date_1), item,
8 MAX(data) KEEP (DENSE_RANK FIRST ORDER BY date_1) DATA
9 FROM a
10 WHERE date_1 BETWEEN add_months(SYSDATE, 6)
11 AND last_day(add_months(SYSDATE, 7))
12 GROUP BY item;
MIN(DATE_1) ITEM DATA
----------- ---- ----
01/06/2013 1 4
22/05/2013 2 3
This will select the (MAX) data on the subset of row that has the lowest date (here only one row).

Resources