oracle sql query giving error - sql

Following query is giving error:
SELECT to_char(last_day(add_months(to_char(to_date('01-02-2013','dd-mm-yyyy'),
'dd-MON-yyyy'),-1)) + 1,'dd-mm-yyyy') FROM dual;
ORA-01858: a non-numeric character was found where a numeric was expected
I tried this on two systems:
with NLS_DATE_FORMAT='DD-MON-RR' - this query works fine.
With NLS_DATE_FORMAT='MM-DD-YYYY' - gives me error ORA-01858: a non-numeric character was found where a numeric was expected.
Any clues as to why this query is failing? I can't have the queries be dependent on the DATE format.

Why are you doing a to_char when calling add_months , you need to pass a date like
SELECT to_char(last_day(add_months(to_date('01-02-2013','dd-mm-yyyy'),
,-1)) + 1,'dd-mm-yyyy') FROM dual;

You have an implicit char-to-date conversion, in the add_months() call; the argument you're passing is a string, not a date. The to_char() you have inside that is redundant, and causing the error when your NLS_DATE_FORMAT doesn't match the format you're using in that to_char():
SELECT to_char(last_day(add_months(to_date('01-02-2013','dd-mm-yyyy'),-1)) + 1,
'dd-mm-yyyy') FROM dual;
I'm not entirely sure what you're doing though... if you want the first day of the month that date is in, you can do this:
SELECT to_char(trunc(to_date('01-02-2013', 'dd-mm-yyyy'), 'MM'),
'dd-mm-yyyy') FROM dual;
This uses the TRUNC(date) function to effectively round down to the start of the month.

Related

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

TO_ CHAR in oracle

I am using oracle 11G and I know TO_CHAR is used to convert data into character and this query will work perfectly to get the day of the particular date:
SELECT TO_CHAR (DATE '2019-08-15', 'Day') FROM DUAL;
But this won’t work:
SELECT TO_CHAR (DATE '15-08-2019', 'Day') FROM DUAL;
ERROR: ORA-01847: day of month must be between 1 and last day of month
SELECT TO_CHAR (DATE '15-aug-19', 'Day') FROM DUAL;
ERROR: ORA-01843: not a valid month
Why the above won’t work. I have checked NLS_DATE_FORMAT it displays “DD-MON-RR”. But in first query it’s “YYYY-MM-DD”.
What am I missing here?
Because YYYY-MM-DD is the string literal format in date'YYYY-MM-DD' with respect to ANSI 92 SQL standard.
For other format types, you need to convert explicitly, such as to_date('15-08-2019','dd-mm-yyyy')
This issue is purely due to your NLS settings. if you run below query you can see the current NLS parameter values for your database
SELECT * FROM NLS_DATABASE_PARAMETERS
if you want to change your nls parameter for date please run below query
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MM-YYYYY';

ORA-01861 error on NUMBER(8) column

I have the following SQL, which basically checks whether the "SYSDATE" fits in between specific dates(i.e. some date <= SYSDATE <= some date + 30 days).
TO_DATE(TO_CHAR(SYSDATE, 'YYYYMMDD')) BETWEEN TO_DATE(TO_CHAR(T1.WEB_STR_DT, 'YYYYMMDD')) AND
TO_DATE(TO_CHAR(T1.WEB_STR_DT, 'YYYYMMDD')) + 30
The problem I am facing is that I get ORA-01861 even the date is in the valid date format(yyyymmdd).
What could be the cause of this error?
You shouldn't be converting sysdate to and from a string at all, as that is wasteful but is also doing implicit conversion - which is what is causing the ORA-018611 error. Your NLS_DATE_FORMAT is not YYYYMMDD; you explicitly convert the date to that format, then try to convert the resulting string back to a date using whatever your NLS settings currently are:
select to_date(to_char(sysdate, 'YYYYMMDD')) from dual;
ORA-01861: literal does not match format string
select to_date(to_char(sysdate, 'YYYYMMDD'), 'YYYYMMDD') from dual;
TO_DATE(T
---------
13-APR-17
But instead of that you can just use trunc(sysdate). That strips the time back to midnight, so it would match if WEB_STR_DT represented today.
That isn't the only problem though. You shouldn't be storing dates as numbers in the first place, but if you must convert a number like 20170413 to a date then you need to convert it to a string first and then convert that string to a date.
Essentially you have your parentheses in the wrong place, and instead of
TO_DATE(TO_CHAR(T1.WEB_STR_DT, 'YYYYMMDD'))
you should have:
TO_DATE(TO_CHAR(T1.WEB_STR_DT), 'YYYYMMDD')
Your code is doing the equivalent of:
select to_date(to_char(20170413, 'YYYYMMDD')) from dual;
ORA-01481: invalid number format model
because you're attempting to use date format model elements to convert a number to a string. With that parenthesis moved you do end up with the equivalent date.
So, you can do:
TRUNC(SYSDATE) BETWEEN TO_DATE(TO_CHAR(T1.WEB_STR_DT), 'YYYYMMDD')
AND TO_DATE(TO_CHAR(T1.WEB_STR_DT), 'YYYYMMDD') + 30
Even the explicit TO_CHAR() calls are a bit unnecessary, since you aren't supply a format model for those, so even simpler would be:
TRUNC(SYSDATE) BETWEEN TO_DATE(T1.WEB_STR_DT, 'YYYYMMDD')
AND TO_DATE(T1.WEB_STR_DT, 'YYYYMMDD') + 30
Number conversion issues (and what is to_chat?)... I assume you just want date with no time.
Try TRUNC:
WHERE trunc(sysdate) between trunc(T1.WEB_STR_DT) and trunc(T1.WEB_STR_DT)+30

character to date in oracle sql

What is the error in below SQL:
select to_Date(substr('2/22/2015 9:20:06 AM',1,9),'mm/dd/yyyy','DD-MON-YY') from dual
I am getting the following error:
ORA-12702: invalid NLS parameter string used in SQL function error
But select substr('2/22/2015 9:20:06 AM',1,9) from dual shows 2/22/2015. I want to convert this as date.
to_Date(substr('2/22/2015 9:20:06 AM',1,9),'mm/dd/yyyy','DD-MON-YY')
Break the query into individual parts and then understand.
substr and to_date are the two functions being used.
Extracting the required substring, substr('2/22/2015 9:20:06 AM',1,9)
Applying TO_DATE over the output of step 1, TO_DATE(substr('2/22/2015 9:20:06 AM',1,9),'mm/dd/yyyy').
And, that's it.
So, in your posted query, 'DD-MON-YY' format mask is not required at all.
Also, what you are trying to achieve is equivalent to:
TRUNC(TO_DATE('2/22/2015 9:20:06 AM', 'MM/DD/YYYY HH:MI:SS AM'))
TRUNC removes the time portion from a DATE type.
Alternatively,
You could use the ANSI TIMESTAMP literal:
TRUNC(TIMESTAMP '2015-02-22 09:20:06')
Remove the third parameter from your TO_DATE method:
select to_date(substr('2/22/2015 9:20:06 AM',1,9),'mm/dd/yyyy') from dual
This is the nlsparam (see documentation).

how to convert YYYYMMDD to DD-Mon-YYYY in oracle?

I am trying to convert the date from YYYYMMDD to DD-Mon-YYYY in Oracle, but to_char or to_Date is not working. Can you please advise?
select to_date(20150324,'DD-Mon-YY') from dual;
select to_char(20150324,'DD-Mon-YY') from dual;
I get an error message saying: - ORA-01861: literal does not match format string
Use this combination of to_char and to_date:
select to_char (to_date('20150324','YYYYMMDD'), 'DD-Mon-YY') from dual;
Your mistake was, that you used the wrong date pattern. Additionally it's recommended to add'', though it worked without them in this case.
Check this Fiddle.