Convert short month names into number - sql

I have dates like this and I want to convert those short month names into numbers.
Sep 30 2021 19:00:04 +08
My desired output is either one of them.
2021/30/09 19:00:04 or 20213009190004

One option
SQL> with x ( string ) as ( select 'Sep 30 2021 19:00:04 +08' from dual )
2 select to_char(to_date(substr(string,0,length(string) - 3),'Mon DD YYYY HH24:MI:SS'),'YYYY/MM/DD HH24:MI:SS') from x ;
TO_CHAR(TO_DATE(SUB
-------------------
2021/09/30 19:00:04
SQL> with x ( string ) as ( select 'Sep 30 2021 19:00:04 +08' from dual )
2 select to_char(to_date(substr(string,0,length(string) - 3),'Mon DD YYYY HH24:MI:SS'),'YYYYMMDDHH24MISS') from x ;
TO_CHAR(TO_DAT
--------------
20210930190004

Related

How to get first date and last date of all twelve months for given year in Oracle PLSQL?

If i give input year like '2021' i need result as below
Month Start Date End Date
1 1/1/2021 31/01/2021
2 1/2/2021 28/01/2021
.
.
.
.
.
.
.
.
.
12 1/12/2021 31/12/2021
Basically, it is about the row generator technique; there are plenty of them, pick any you want. (Have a look at OraFAQ).
For example:
SQL> with mon as
2 (select add_months(trunc(to_date(&par_year, 'yyyy'), 'yyyy'), level - 1) val
3 from dual
4 connect by level <= 12
5 )
6 select to_char(val, 'mm') mon,
7 val start_date,
8 last_day(val) end_date
9 from mon
10 order by 1;
Enter value for par_year: 2021
MO START_DATE END_DATE
-- ---------- ----------
01 01/01/2021 31/01/2021
02 01/02/2021 28/02/2021
03 01/03/2021 31/03/2021
04 01/04/2021 30/04/2021
05 01/05/2021 31/05/2021
06 01/06/2021 30/06/2021
07 01/07/2021 31/07/2021
08 01/08/2021 31/08/2021
09 01/09/2021 30/09/2021
10 01/10/2021 31/10/2021
11 01/11/2021 30/11/2021
12 01/12/2021 31/12/2021
12 rows selected.
SQL>
You could also use directly the model clause for that purpose.
SELECT n
, TO_DATE(&the_year||lpad(f, 2, '0'), 'YYYYMM') start_dt
, last_day(TO_DATE(&the_year||lpad(f, 2, '0'), 'YYYYMM')) end_dt
FROM DUAL
MODEL
DIMENSION by (1 as n)
MEASURES (1 as f)
RULES (
f[FOR n FROM 1 TO 12 INCREMENT 1 ] = cv(n)
)
;
The advantage of the model clause is if you later want to get the every other month, you just need to change the increment from 1 to 2. Or if you are looking for the quarter months of the year (January, April, Jully, October), you just need to change increment from 1 to 3, and so on...
Just replace 2021 in the query for your year
with months (m) as (
select 1 from dual union all
select m + 1 from months where m < 12
)
select
to_date('2021' || '-' || to_char(m) || '-01', 'YYYY-MM-DD') as first_day,
last_day(to_date('2021' || '-' || to_char(m) || '-01', 'YYYY-MM-DD')) as last_day
from months
You can try on this db<>fiddle
Try below query
WITH cte_date as(
SELECT
LEVEL Month_No,
to_date(to_char(LEVEL||'-2021'),'MM-YYYY') Start_Date FROM dual
CONNECT BY LEVEL <=12
)
SELECT Month_No, Start_Date, LAST_DAY(Start_Date) End_Date
FROM cte_date;
I'm a fan of recursive CTEs because they are part of Standard SQL. I would phrase this as:
with months (month, startdate) as (
select 1 as month, date '2021-01-01'
from dual
union all
select month + 1, add_months(startdate, 1)
from months
where month < 12
)
select month, startdate, last_day(startdate) as enddate
from months;
If you need an input year, there are different ways to accomplish it. But a simple way is to change the second line to:
select 1 as month, to_date(:year || '0101', 'YYYYMMDD')

Add subquery in query - Oracle SQL

My query return all days in month.
SELECT
EXTRACT( DAY FROM day ) ||' '|| substr(TO_CHAR( day, 'fmDAY' ),0,3) AS day,
EXTRACT( DAY FROM day ) as day_id
FROM (
WITH temp ( col ) AS (
SELECT to_date(2, 'mm') --2 is February
FROM dual
)
SELECT col + level - 1 AS day
FROM temp
CONNECT BY level <= last_day(col) - col + 1
ORDER BY day
)
How get all days from query where DAY_ID not in ==> (Select day_id from table1)
Eg. table1 return 5,10,15
Query resault need to display all days except 5,10,15
You can generate your calendar and then use NOT EXISTS:
WITH month ( col ) AS (
SELECT ADD_MONTHS(TRUNC(SYSDATE, 'YYYY'), 2 - 1)
FROM DUAL
),
calendar ( day, end_day ) AS (
SELECT col, LAST_DAY(col)
FROM month
UNION ALL
SELECT day + INTERVAL '1' DAY, end_day
FROM calendar
WHERE day < end_day
)
SELECT EXTRACT( DAY FROM day ) ||' '|| TO_CHAR( day, 'fmDY' ) AS day,
EXTRACT( DAY FROM day ) as day_id
FROM calendar c
WHERE NOT EXISTS (
SELECT 1
FROM table1 t
WHERE c.day = t.day_id
)
ORDER BY c.day;
Which, for your sample data:
CREATE TABLE table1 ( day_id ) AS
SELECT DATE '2021-02-05' FROM DUAL UNION ALL
SELECT DATE '2021-02-10' FROM DUAL UNION ALL
SELECT DATE '2021-02-15' FROM DUAL;
Outputs:
DAY
DAY_ID
1 MON
1
2 TUE
2
3 WED
3
4 THU
4
6 SAT
6
7 SUN
7
8 MON
8
9 TUE
9
11 THU
11
12 FRI
12
13 SAT
13
14 SUN
14
16 TUE
16
17 WED
17
18 THU
18
19 FRI
19
20 SAT
20
21 SUN
21
22 MON
22
23 TUE
23
24 WED
24
25 THU
25
26 FRI
26
27 SAT
27
28 SUN
28
db<>fiddle here
One way to generate all rows in month for dates in another table is to use a recursive CTE:
create table table1 as
select date '2021-04-16' as day_in from dual;
with cte (dte) as (
select trunc(day_in, 'MON') as dte
from table1
union all
select dte + interval '1' day
from cte
where dte < last_day(dte)
)
select *
from cte;
Here is a db<>fiddle.
EDIT:
If you want the days not in a table, then use:
with cte (dte) as (
select date '2021-02-01' as dte
from dual
union all
select dte + interval '1' day
from cte
where dte < last_day(dte)
)
select dte
from cte
where not exists (select 1 from table1 t1 where t1.day_id = cte.dte);
You can also use not exists using your query, but I find the recursive CTE easier to follow -- and it is standard SQL.

Query Optimization for my code in Oracle SQL

Here is my code:
select
(case when c.yr = 2019 and c.mon = 10 then 'October 2019'
when c.yr = 2019 and c.mon = 11 then 'November 2019'
when c.yr =2019 and c.mon = 12 then 'December 2019' end) as dae
from (
select substr(d,-4,4) as yr, substr(d,1,2) as mon
from
(select '10/11/2019' as d from dual) )c;
`
So I don't want to hard code the dates for the next 5 years, Is there a function that makes this easier.
Here is the Sample Input I want to try
10/11/2019
11/11/2019
12/11/2019
01/11/2020
Expected Output
October 2019
November 2019
December 2019
January 2020
You could use to_date() to turn your string to a date, and then convert it back to a string in the desired format with to_char():
to_char(to_date(d, 'mm/dd/yyyy'), 'Month yyyy')
Demo on DB Fiddle:
with t as (
select '10/11/2019' d from dual
union all select '11/11/2019' from dual
union all select '12/11/2019' from dual
union all select '01/11/2020' from dual
)
select to_char(to_date(d, 'mm/dd/yyyy'), 'Month yyyy') new_dt from t
| NEW_DT |
| :------------- |
| October 2019 |
| November 2019 |
| December 2019 |
| January 2020 |
Use connect by to generate as many dates as you want. Here the gen_dates CTE starts with your start_date and returns a total of 4 months per your example. To increase the number of months to generate, increase the number 4 to a higher number.
with gen_dates(date_in) as (
select add_months('11-OCT-2019', level -1) date_in
from dual
connect by level <= 4
)
select date_in, to_char(date_in, 'Month yyyy') date_out
from gen_dates;
DATE_IN DATE_OUT
--------- --------------
11-OCT-19 October 2019
11-NOV-19 November 2019
11-DEC-19 December 2019
11-JAN-20 January 2020
4 rows selected.

Oracle query to get end of week from the given data set

I have table in Oracle , where EOW columns indicate the end of week.
I want to write a query to get the nearest end of week date.
Table Cal
DAY DAY OFTHE WEEK EOW
20181026 FRI Y
20181027 SAT N
20181028 SUN N
20181029 MON N
20181030 TUE N -->
20181031 WED N
20181101 THU N
20181102 FRI Y -->
20181103 SAT N
So when I
select DAY , "logic" from cal where day = 20181030;
What should be "logic" so that I get the nearest end of week date , in this case
20181026.
Please help!!
Do you really need a fixed table for that? A CTE can easily create any calendar you want, so - I took that freedom to produce something like this.
I wrote it step-by-step so that you could follow its execution. Start from the first CTE (dates), then go to day_diff, and so forth). It seems that you are selecting the first FRI that precedes current date. Because, for 20181030, the nearest end-of-week isn't 20181026 (4 days to that Friday) but 20181102 (3 days to that Friday).
At the end, the result is
SQL> with dates as
2 (select
3 -- add "level" (sequence of numbers from 1 to 60) to 1st of previous month
4 trunc(add_months(sysdate, - 1), 'mm') + level - 1 datum,
5 -- convert that date into a day name (MON, FRI, ...)
6 to_char(trunc(add_months(sysdate, -1), 'mm') + level - 1, 'DY',
7 'NLS_DATE_LANGUAGE=ENGLISH') dan,
8 -- if day name is FRI, set EOW = Y. Else, it is N
9 case when to_char(trunc(add_months(sysdate, -1), 'mm') + level - 1, 'DY',
10 'NLS_DATE_LANGUAGE=ENGLISH') = 'FRI' then 'Y'
11 else 'N'
12 end eow
13 from dual
14 connect by level <= 60 -- my CTE will have 60 dates; yours can have any number
15 ),
16 day_diff as
17 (select datum, dan, eow,
18 datum - to_date('&par_datum', 'dd.mm.yyyy') diff
19 from dates
20 ),
21 diff_only_eow as
22 (select datum, dan, eow, diff,
23 row_number() over (order by diff desc) rn
24 from day_diff
25 where eow = 'Y'
26 and diff <= 0
27 )
28 select datum, dan, eow, diff, rn
29 from diff_only_eow
30 where rn = 1;
Enter value for par_datum: 30.10.2018
DATUM DAN E DIFF RN
-------- ------------ - ---------- ----------
20181026 FRI Y -4 1
SQL>
What I am guessing is,
If the date is 30-OCT-2018 (20181030), then you want last friday date as 26-OCT - 2018 (20181026).
Another Scenario :
If the date is 27-OCT-2018 (20181027), in that case also you want last friday date which is 26-OCT - 2018 (20181026).
If my this is guess is true then below query may work :
WITH TEMP1 AS
(
SELECT TO_CHAR(
TO_DATE('20181030','YYYYMMDD') ,'DD-MON-YY') AS DATE_TEST
FROM DUAL
)
SELECT next_day (TO_DATE(DATE_TEST,'DD-MON-YY')-7,'FRIDAY') Last_Friday
FROM TEMP1;
It will display output as :
LAST_FRIDAY
26-OCT-18
Which you can convert later in your required format.
Test Case 2
WITH TEMP1 AS
(
SELECT TO_CHAR(
TO_DATE('20181103','YYYYMMDD') ,'DD-MON-YY') AS DATE_TEST
FROM DUAL
)
SELECT next_day (TO_DATE(DATE_TEST,'DD-MON-YY')-7,'FRIDAY') Last_Friday
FROM TEMP1;
Output :
LAST_FRIDAY
02-NOV-18
Now breaking of above query :
WITH clause is used in CTE.
WITH TEMP1 AS
(
SELECT TO_CHAR(
TO_DATE('20181103','YYYYMMDD') ,'DD-MON-YY') AS DATE_TEST
FROM DUAL
)
Here, TO_DATE('20181103','YYYYMMDD') ,'DD-MON-YY' - will convert 20181103 as 03-NOV-2018.
So the result from the WITH clause (Which is 03-NOV-2018) will be used in another query :
SELECT next_day (TO_DATE(DATE_TEST,'DD-MON-YY')-7,'FRIDAY') Last_Friday
FROM TEMP1;
Here DATE_TEST is output from with clause. First
TO_DATE(DATE_TEST,'DD-MON-YY')-7
It is taking previous 7 days from the mentioned date (which is currently DATE_TEST : 03-NOV -2018) So It will take all the last 7 days from 03-NOV-2018.
Assuming :
DATE DAY Order
28-10-2018 SUN 1
29-10-2018 MON 2
30-10-2018 TUE 3
31-10-2018 WED 4
01-11-2018 THURS 5
02-11-2018 FRI 6
03-11-2018 SAT 7
We got all 7 days mentioned above.
next_day (TO_DATE(DATE_TEST,'DD-MON-YY')-7,'FRIDAY')
Now from next_day, we can get another day and here, we are asking for FRIDAY by mentioning it in the argument. So FRIDAY is 02-11-2018.
So the output will be 02-11-2018.
This may solve your query
select day, to_char(to_date(day,'YYYYMMDD'),'DY') f1,
case to_char(to_date(day,'YYYYMMDD'),'DY')
when 'FRI' then 0
when 'SAT' then -1
when 'SUN' then -2
when 'MON' then -3
when 'TUE' then 3
when 'WED' then 2
when 'THU' then 1
end as f2,
to_char(to_date(day,'YYYYMMDD')+ case to_char(to_date(day,'YYYYMMDD'),'DY')
when 'FRI' then 0
when 'SAT' then -1
when 'SUN' then -2
when 'MON' then -3
when 'TUE' then 3
when 'WED' then 2
when 'THU' then 1
end,'YYYYMMDD') as f3
from test_cal;
DAY F1 F2 F3
-------- --------- ---------- --------
20181026 FRI 0 20181026
20181027 SAT -1 20181026
20181028 SUN -2 20181026
20181029 MON -3 20181026
20181030 TUE 3 20181102
20181031 WED 2 20181102
20181101 THU 1 20181102
20181102 FRI 0 20181102
20181103 SAT -1 20181102
9 rows selected.

Update entire table with sequence number in oracle

I want to update table with sequence number, I cant create procedure or sequence object so need a update query for this. I have a table with date column, based on minimum date I need to generate a number.
Table Data:-
Date_Value
----------
5th Feb 11
2nd Jan 11
11th Jan 11
After Update :-
SrNo Date_Value
-------------------
1 2nd Jan 11
2 11th Jan 11
3 5th Feb 11
merge into foo
using
(
select rowid as rid,
row_number() over (order by date_value) as seqno
from foo
) t on (foo.rowid = t.rid)
when matched then update
set srno = t.seqno;
SQLFiddle demo: http://sqlfiddle.com/#!4/d8cc5/2
Your example shows that you store dates as a string using custom representation - not compliant with dates format. So we need to convert your dates to date datatype to be able to order them accordingly.
SQL> create table TB_DATES
2 (
3 DATE_VALUE VARCHAR2(11)
4 )
5 /
Table created
SQL>
SQL> insert into tb_dates(date_value)
2 select '5th Feb 11' from dual union all
3 select '2nd Jan 11' from dual union all
4 select '6th Feb 11' from dual union all
5 select '11th Jan 11' from dual
6 ;
4 rows inserted
SQL> commit;
Commit complete
SQL> select * from tb_dates;
DATE_VALUE
-----------
5th Feb 11
2nd Jan 11
6th Feb 11
11th Jan 11
SQL> alter table tb_dates add srno number;
Table altered
QL> select * from tb_dates;
DATE_VALUE SRNO
----------- ----------
5th Feb 11 null
2nd Jan 11 null
6th Feb 11 null
11th Jan 11 null
SQL> merge into tb_dates t
2 using(
3 select rownum rn
4 , Date_value
5 , rid
6 from ( select to_date(concat(lpad(dy, 2, '0'), tr), 'dd Mon yy') dt
7 , Date_value
8 , rid
9 from (select regexp_substr(substr(Date_value, 1, regexp_instr(date_value, '[[:space:]]') - 1), '[[:digit:]]+') dy
10 , substr(Date_value, regexp_instr(date_value, '[[:space:]]'), length(Date_value)) tr
11 , Date_value
12 , rowid rid
13 from tb_dates)
14 order by 1
15 ) ) q
16 on (q.rid = t.rowid)
17 when matched
18 then update set t.srno = q.rn
19 ;
Done
SQL> commit;
Commit complete
select * from tb_dates;
DATE_VALUE SRNO
----------- ----------
5th Feb 11 3
2nd Jan 11 1
6th Feb 11 4
11th Jan 11 2
SQL> select * from tb_dates order by srno;
DATE_VALUE SRNO
----------- ----------
2nd Jan 11 1
11th Jan 11 2
5th Feb 11 3
6th Feb 11 4