list of previous month - sql

How do I add date format (DD-MM-YYYY) in this query? I've added date format after sysdate but it shows missing right parenthesis error.
select EXTRACT(MONTH FROM (SYSDATE,'DD-MM-YYYY'))-level mn1
from dual
connect by level<4;
This should produce output like the following:
01-NOV-15 01-OCT-15 01-SEP-15

You should use ADD_MONTHS() and then TRUNC() to first date. Finally, convert to the format to ant using TO_CHAR()
select TO_CHAR(TRUNC(ADD_MONTHS(SYSDATE,-level),'MONTH'),'DD-MM-YYYY') mn1
from dual
connect by level<4;

Try in this format.
select EXTRACT(MONTH FROM (SYSDATE,'DD-MM-YYYY')-level) mn1
from dual
connect by level<4;

Related

How to get Year/Week according to ISO?

I have the below code, which gives week 202153. However, I would need to have, instead of the first week of year 2021 as 202153, as 202053.
Is there any built-in functionality to address this?
SELECT
to_char(TO_DATE('20210104', 'YYYYMMDD') - 3, 'YYYYIW') AS yearweek
FROM
dual
Expected output: 202053. I could simply decode the value, but I'm hoping for a more robust solution for future years.
Use IYYY (ISO-year) rather than the YYYY (calendar year) format model.
SELECT TO_CHAR( DATE '2021-01-04' - 3, 'IYYYIW') AS yearweek
FROM DUAL
Which outputs:
YEARWEEK
202053
db<>fiddle here

How to convert system date to YYYYMM format by using SQL Query?

I need to convert sysdate in YYYYMM format to validate the expiry date, this is the query I have written but am facing the below error.
select to_char (to_date(sysdate,'MM/DD/YYYY'),'YYYYMM') from dual;
ORA-01858: a non-numeric character was found where a numeric was expected
There is no reason to convert sysdate to a date. So just use:
select to_char(sysdate, 'YYYYMM')
If you wanna to format the date, try "DATE_FORMAT" way:
SELECT DATE_FORMAT('2011-11-11 13:14:01','%m%Y')
or you wanna to get the difference between the expired date and sysdate,you may try "DATEDIFF":
SELECT DATEDIFF('2001-01-01','2001-02-02') -> -32

Retrieve number of the day sql query

I wan't to retrieve the number of the day using an SQL request but I found that I retrieve the number of day since the 01-01-4712 using this query:
SELECT TO_CHAR(TRUNC(SYSDATE),'J') FROM DUAL;
Is there any other query I may use?
Use to_char() to convert your date. Format mask 'DDD' returns the day of the year.
select to_char(sysdate, 'DDD') from dual;
select to_char(to_date('1/1/2014', 'MM/DD/YYYY'), 'DDD') from dual;
Some more info on various formats: http://www.techonthenet.com/oracle/functions/to_char.php
After investigations I found that we can use this query to obtain the number of day in the current year:
select to_date(sysdate) - trunc(sysdate,'YYYY') from dual

error with date in query?

I need some help with this query:
update MSG_TRACE set MSG_SENT_STATUS = 'INIT',ERROR_CODE = 0,RETRY_COUNT = 0 where MSG_RECEIVED_TIME >= '16-01-2012 00:00:00,000000' and OPER_TXN_ID like 'CAP%' and MSG_SENT_STATUS in ('FAILED','ERROR');
It gives me this error: ORA-01843: not a valid month
The MSG_RECEIVED_TIME is in the format DD/MM/YYYY HH:MM:SS.mmmmmm
like 16/01/2012 02:46:34.729643 PM
thank you very much in advance
The milliseconds part means it is not a date. You need to cast to a timestamp such as:
select to_timestamp('16-01-2012 00:00:00,000000','dd-mm-yyyy hh24:mi:ss,FF')
from dual
If you need it to be a date format then you can cast it further:
select to_date(
to_char(
to_timestamp('16-01-2012 00:00:00,000000'
,'dd-mm-yyyy hh24:mi:ss,FF')
,'dd-mm-yyyy hh24:mi:ss')
,'dd-mm-yyyy hh24:mi:ss')
from dual
Your data base configured to be MM/DD/YYYY so 16/01/2012 isn't valid because there is not 16 month. you have three options:
Change 16/01/2012 to 01/16/2012.
Change your DB to be DD/MM/YYYY
Try add the 16 month to our calenders... =)
'01-16-2012 00:00:00,000000' is incorrect... try with '16-01-2012 00:00:00'
Also your date is INCORRECT...
ORA-01843: not a valid month error is coming as you are setting month as 16 which is INCORRECT...
Date format in timestamp is as below
DD/MM/YYYY HH:MM:SS:MM

Extract number from the date in pl sql

Does anyone know if it is possible to take the date from a random date in pl sql.
example.
SELECT SYSDATE FROM DUAL
and here the output would be say : 26-10-2010 13:30:34
Now I want to have just the date as a number. In this case that would be 26.
Or is there some sort of function like IsNum that can recognize it for me. So I can just take 26 and leave the rest out.
You can also use EXTRACT(), like so:
SELECT EXTRACT(DAY FROM SYSDATE) AS DAY FROM DUAL;
select to_char(sysdate,'DD') as day from dual
You can use
to_char(SYSDATE, 'DD')
More you can read here: LINK
All format models are described in the official SQL Reference, take a look in case you need something else
select count(*) from
(select sysdate+rownum dates from dual connect by rownum < (sysdate+15)-(sysdate))
where to_char(dates,'D') <> '1' and to_char(dates,'D') <> '7'