ORA-01830 error with to_date - sql

I'm trying to create a date subtract two days and then convert it to a char. For some reason I'm getting the following error:
ORA-01830: date format picture ends before converting entire input string
Here's my code:
SELECT TO_CHAR(to_date('20-JUL-01 10:40:12')-2, 'dd-Mon-yy 24HH:MI:SS') as "Subtract 2 Days"
FROM DUAL;
I'm not sure what's wrong, it seems to be an issue with the seconds

The Oracle default is a 12-hour clock with AM/PM. So, you need a date format for the date conversion:
SELECT TO_CHAR(to_date('20-JUL-01 10:40:12', 'dd-Mon-yy HH24:MI:SS')-2,
'dd-Mon-yy HH24:MI:SS') as "Subtract 2 Days"
FROM DUAL;
Also, the correct 24-hour signifier is "HH24", not "24HH".

Related

SQL Query Error: date format picture ends before converting entire input into string

I'm getting an Error when I run the query below:
to_date('30-APR-19 09.53.35.000000 AM', 'DD-Mon-yy hh24.mi.ss')
Date format picture ends before converting entire input into string
Can I get an assistance please
The major problem you've got is that your date-and-time string can't be parsed using TO_DATE - you'll need to use TO_TIMESTAMP. The issue is that TO_DATE doesn't recognize the FFn format specifier, which is used to process fractional seconds. This makes sense because DATE values are only accurate to the second. So you'll need to use
TO_TIMESTAMP('30-APR-19 09.53.35.000000 AM', 'DD-MON-YY HH.MI.SS.FF6 AM')
Which will return a TIMESTAMP value. If you really need this to be a DATE rather than a TIMESTAMP you can cast the value to DATE by using
CAST(TO_TIMESTAMP('30-APR-19 09.53.35.000000 AM', 'DD-MON-YY HH.MI.SS.FF6 AM') AS DATE)
dbfiddle here
You can directly use to_date function and miliseconds can be ignored using # as following:
to_date('30-APR-19 09.53.35.000000 AM', 'DD-MON-YY HH.MI.SS.###### AM')
Number of # is equal to number of 0s after dot(.)
db<>fiddle demo
Cheers!!

01830. 00000 - "date format picture ends before converting entire input string"

select to_date('13/03/17 05:43:29,000000000 PM -05:00DD/MM/YY HH24:MI:SS') from
irregularities;
How to convert this date to 24-hour format?
You can convert a string to a timestamp with time zone using:
select to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM')
from dual;
If you only want a date data type then you can cast it:
select cast(
to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM')
as date)
from dual;
If you really only want the string version you can convert it back, which you would usually only do for display:
select to_date(
to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM'),
'DD/MM/YYYY HH24:MI:SS')
from dual;
If the original string is coming from a table then just replace the text literal with the column name, and dual with your table name. Of course, that assumes the column is actually a string. If it is actually already a timestamp and your client is just displaying it in a way you don't like, you only need theto_char() part.
Read more about these things in the documentation: to_timestamp_tz, format models, cast() and to_char().

ORA-01843: not a valid month with data type conversion

I have created a view where one of column command is:
TO_CHAR( TO_DATE(sysdate ||' '||TIMING.TIME,'dd-MON-RRRR HH:MIAM'),'dd-MON-RRRR HH:MIAM') as time
The value of TIMING.TIME is like this: 09:30AM as varchar2
When I run the query: select TO_DATE(time,'DD-MON-RRRR HH:MIAM')from view
I get the error
ORA-01843: not a valid month
NLS Language is American.
TO_DATE(sysdate
That is wrong.
Never apply TO_DATE on DATE data type. It forces Oracle to:
first convert it into a string
then convert it back to date
based on the locale-specific NLS settings. You need TO_DATE to convert a literal into date. For date-arithmetic, leave the date as it is.
If you are trying to configure the time portion in current date, then:
1. First convert the date into string
2. Then concatenate the time portion to the string
3. Finally apply TO_DATE
For example,
SQL> alter session set nls_date_format='DD-MM-YYYY HH:MI:SS AM';
Session altered.
SQL> SELECT to_date(TO_CHAR(sysdate, 'mm/dd/yyyy')
2 ||' '
3 ||'09:30AM', 'mm/dd/yyyy hh:miAM') TIME
4 FROM dual;
TIME
----------------------
14-10-2015 09:30:00 AM
Remember,
TO_DATE is used to convert a string into date.
TO_CHAR is used to display the date in desired string format.
Modified query for you:
to_date(TO_CHAR(sysdate, 'mm/dd/yyyy') ||' ' ||TIMING.TIME, 'mm/dd/yyyy hh:miAM')
AS "TIME"
sysdate stores time and it would be better to convert it to char before concatenation
TO_CHAR(sysdate,'dd-MON-RRRR')||' '||TIMING.TIME

convert string literal to a date

I have a varchar2 field in my db with the format of for example -
2015-08-19 00:00:01.0
2014-01-11 00:00:01.0
etc.
I am trying to convert this to a date of format DD-MON-YYYY. For instance, 2015-08-19 00:00:01.0 should become 19-AUG-2015. I've tried
select to_date(upgrade_date, 'YYYY-MM-DD HH24:MI:SS') from connection_report_update
but even at this point I'am getting ORA-01830 date format ends before converting the entire input string. Any ideas?
You have details upto milli seconds, for which, you have to use TO_TIMESTAMP() with format model 'FF'
select to_timestamp('2015-08-19 00:00:01.0' ,'YYYY-MM-DD HH24:MI:SS.FF') as result from dual;
RESULT
---------------------------------------------------------------------------
19-AUG-15 12.00.01.000000000 AM
And Date doesn't have a format itself, only the date output can be in a format. So, when you want it to be printed in a different format, you would need to again use a TO_CHAR() of the converted timestamp;
select to_char(to_timestamp('2015-08-19 00:00:01.0' ,'YYYY-MM-DD HH24:MI:SS.FF'),'DD-MON-YYYY') as result from dual;
RESULT
-----------
19-AUG-2015
Why do you store datetimes in a string???
Anyhow. To get from '2015-08-19 00:00:01.0' to a datetime with milliseconds (which is a TIMESTAMP in Oracle) use to_timestamp:
to_timestamp('2015-08-19 00:00:01.0', 'yyyy-mm-dd hh24:mi:ss.ff')
Then to get the desired output format, use to_char:
to_char(thedate, 'DD-MON-YYYY')
Together:
to_char(to_timestamp('2015-08-19 00:00:01.0', 'yyyy-mm-dd hh24:mi:ss.ff'), 'DD-MON-YYYY')
You should be specifying the format that you want in the call of to_date not the current format:
select to_date(upgrade_date, 'DD-MM-YYYY') from connection_report_update

Checking format of timestamp with time zone variable in oracle plsql

I need to create a oracle plsql script which have input param v_date as Timestamp with time zone type.
Also I need to verify whether the format of the v_date is as per mentioned format 'YYYY-MM-DD HH24:MI:SS TZH:TZM' or not .
How should I achieve this?
I was thinking of achieveing this by changing input param v_date as String and then convert this string into Timestamp with time zone and if any error occured while converting then conclude that the format is wrong. But it seems TO_TIMESTAMP_TZ functions doesn't throw error when the date string is in different format w.r.t the specified format instead it converts it.
For e.g.
SELECT TO_TIMESTAMP_TZ('15-APR-15',
'YYYY-MM-DD HH:MI:SS TZH:TZM') FROM DUAL;
Actual result : 14-APR-15 11.50.00.000000000 PM AMERICA/CHICAGO
Expected result : Some error as format is not as per 'YYYY-MM-DD HH:MI:SS TZH:TZM'
In short I want to know how to verify the format of Timestamp with time zone type.
You could try adding the "fx" modifier to the format, which says to Oracle "expect this exact format":
SELECT TO_TIMESTAMP_TZ('15-APR-15 ', 'fxYYYY-MM-DD HH:MI:SS TZH:TZM') FROM DUAL;
SELECT TO_TIMESTAMP_TZ('15-APR-15 ', 'fxYYYY-MM-DD HH:MI:SS TZH:TZM') FROM DUAL
*
Error at line 1
ORA-01862: the numeric value does not match the length of the format item