Not able to perform TO_TIMESTAMP on my date field.
select TO_TIMESTAMP( mydate ,'YYYY-MM-DD HH24:MI:SS') as mydate from cte3
Error Msg.
ERROR: invalid value "None" for "YYYY"
DETAIL: Value must be an integer.
SQL state: 22007```
Related
Both of following SQL works in my Oracle DB, the result or the count of results are completely same and correct. CREATE_TIME is a timestamp column.
select * from EMPLOYEE where CREATE_TIME =(or >) '2022-01-21 2:49:38.251'
select * from EMPLOYEE where CREATE_TIME =(or >) '22-01-21 2:49:38.251000'
Quite curious on how it works because the String format is different, I didn't use TO_* conversion function and the Oracle NLS(as default conversion rules) shows completely different format.
PARAMETER -> VALUE
NLS_TIMESTAMP_TZ_FORMAT -> DD-MON-RR HH.MI.SSXFF AM TZR
NLS_TIME_TZ_FORMAT -> HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_FORMAT -> DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_FORMAT -> HH.MI.SSXFF AM
Searched the information but didn't find the answer, it would be appreciate if anyone could answer me and provide a information/document link for reference.
If you try to perform an explicit cast using CAST(value AS TIMESTAMP), or an equivalent implicit cast such as in your query, from the string values to a timestamp then Oracle will implicitly convert the cast to the equivalent of:
TO_TIMESTAMP(
value,
(SELECT value FROM NLS_SESSION_PARAMETERS WHERE parameter = 'NLS_TIMESTAMP_FORMAT')
)
If you have the sample data:
CREATE TABLE table_name (value) AS
SELECT '2022-01-21 2:49:38.251' FROM DUAL UNION ALL
SELECT '22-01-21 2:49:38.251000' FROM DUAL;
You can see it in action using:
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'DD-MON-RR HH24.MI.SSXFF';
SELECT value,
TO_CHAR(
CAST(value AS TIMESTAMP),
'YYYY-MM-DD HH24:MI:SS.FF'
) AS converted_value
FROM table_name;
Outputs the error:
ORA-01843: not a valid month
As, given the string-to-date conversion rules the MON format model will also match MONTH but it will not match the numeric MM format so the 01 month generates the error.
However:
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'RR-MM-DD HH24:MI:SSXFF'
SELECT value,
TO_CHAR(
CAST(value AS TIMESTAMP),
'YYYY-MM-DD HH24:MI:SS.FF'
) AS converted_value
FROM table_name;
Outputs:
VALUE
CONVERTED_VALUE
2022-01-21 2:49:38.251
2022-01-21 02:49:38.251000
22-01-21 2:49:38.251000
2022-01-21 02:49:38.251000
Both the rows are converted as expected.
If, instead, you use:
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SSXFF'
SELECT value,
TO_CHAR(
CAST(value AS TIMESTAMP),
'YYYY-MM-DD HH24:MI:SS.FF'
) AS converted_value
FROM table_name;
Then the output is:
VALUE
CONVERTED_VALUE
2022-01-21 2:49:38.251
2022-01-21 02:49:38.251000
22-01-21 2:49:38.251000
0022-01-21 02:49:38.251000
And the first row converts as expected but the second row the YYYY format model matches 22 and gives 22 AD rather than 2022 AD (as was probably expected).
If you want to compare to a timestamp then either use an explicit conversion:
SELECT *
FROM EMPLOYEE
WHERE CREATE_TIME = TO_TIMESTAMP(
'2022-01-21 2:49:38.251',
'YYYY-MM-DD HH24:MI:SSXFF'
)
Or a timestamp literal:
SELECT *
FROM EMPLOYEE
WHERE CREATE_TIME = TIMESTAMP '2022-01-21 02:49:38.251'
If you rely on the NLS parameters then your query may have different (and unexpected) behaviours for different sessions (sometimes even for different sessions of the same user).
db<>fiddle here
I have column exp_date as date format with NLS value as DD-MON-YYYY HH24:MI:SS in SQL developer,
I am using select to_date(exp_date,'DD-MON-YYYY HH24:MI:SS') from demo;
Why I am not facing any error if to_date function only accepts string as paramater.
Oracle tries to be helpful and implicitly converts exp_date to a string to match the expected first argument of TO_DATE. So, your query:
select to_date(exp_date,'DD-MON-YYYY HH24:MI:SS') from demo;
Is an equivalent of the more explicit date-to-string conversion in this query:
SELECT TO_DATE(
TO_CHAR(
exp_date,
( SELECT value
FROM NLS_SESSION_PARAMETERS
WHERE parameter = 'NLS_DATE_FORMAT'
)
),
'DD-MON-YYYY HH24:MI:SS'
)
FROM demo;
Where the exp_date value has been cast to a string. It works because it just so happens that the NLS_DATE_FORMAT and your format model for TO_DATE are the same.
I am not able to obtain the date object from date string 2019-01-21T19:02:25Z
select to_char(to_date('2019-01-21T19:02:25Z','yyyy-mm-ddThh24:mi:ssZ'),'dd/mm/yyyy hh24:mi:ss') from dual;
yields
ORA-01821: date format not recognized
01821. 00000 - "date format not recognized"
*Cause:
*Action:
May I know what date format is used.
Either use quotes to match the T and Z as literals:
SELECT TO_CHAR(
TO_DATE(
'2019-01-21T19:02:25Z',
'yyyy-mm-dd"T"hh24:mi:ss"Z"'
),
'dd/mm/yyyy hh24:mi:ss'
)
FROM DUAL;
or, match the T as a literal and use TO_TIMESTAMP_TZ with the TZH and TZM format models to match the time zone hours and minutes components (or, instead, TZR to match the time zone region):
SELECT TO_CHAR(
TO_TIMESTAMP_TZ(
'2019-01-21T19:02:25Z',
'yyyy-mm-dd"T"hh24:mi:ssTZHTZM'
),
'dd/mm/yyyy hh24:mi:ss'
)
FROM DUAL;
db<>fiddle
How can I convert this string date to datetime in oracle.
2011-07-28T23:54:14Z
Using this code throws an error:
TO_DATE('2011-07-28T23:54:14Z', 'YYYY-MM-DD HH24:MI:SS')
How can this be done?
Error report:
SQL Error: ORA-01861: literal does not match format string
01861. 00000 - "literal does not match format string"
*Cause: Literals in the input must be the same length as literals in
the format string (with the exception of leading whitespace). If the
"FX" modifier has been toggled on, the literal must match exactly,
with no extra whitespace.
*Action: Correct the format string to match the literal.
Update:-
TO_DATE('2011-07-28T23:54:14Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')
I only see the date not time in the column
28-JUL-11
Try this:
TO_DATE('2011-07-28T23:54:14Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')
Hey I had the same problem. I tried to convert '2017-02-20 12:15:32' varchar to a date with TO_DATE('2017-02-20 12:15:32','YYYY-MM-DD HH24:MI:SS') and all I received was 2017-02-20 the time disappeared
My solution was to use TO_TIMESTAMP('2017-02-20 12:15:32','YYYY-MM-DD HH24:MI:SS') now the time doesn't disappear.
You can use a cast to char to see the date results
select to_char(to_date('17-MAR-17 06.04.54','dd-MON-yy hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') from dual;
When I try to execute this snippet:
cmd.CommandText = "SELECT alarm_id,definition_description,element_id,
TO_CHAR (alarm_datetime, 'YYYY-MM-DD HH24:MI:SS'),severity,
problem_text,status FROM aircom.alarms
WHERE status = 1 and
TO_DATE (alarm_datetime,'DD.MM.YYYY HH24:MI:SS') > TO_DATE ('07.09.2008
09:43:00', 'DD.MM.YYYY HH24:MI:SS')
order
by ALARM_DATETIME desc";
I get:
ORA-01861: literal does not match format string
There is no problem with database connection because I can execute basic SQL commands.
What is the problem with this statement?
Remove the TO_DATE in the WHERE clause
TO_DATE (alarm_datetime,'DD.MM.YYYY HH24:MI:SS')
and change the code to
alarm_datetime
The error comes from to_date conversion of a date column.
Added Explanation: Oracle converts your alarm_datetime into a string using its nls depended date format. After this it calls to_date with your provided date mask. This throws the exception.
The error means that you tried to enter a literal with a format string, but the length of the format string was not the same length as the literal.
One of these formats is incorrect:
TO_CHAR(t.alarm_datetime, 'YYYY-MM-DD HH24:MI:SS')
TO_DATE(alarm_datetime, 'DD.MM.YYYY HH24:MI:SS')
SELECT alarm_id
,definition_description
,element_id
,TO_CHAR (alarm_datetime, 'YYYY-MM-DD HH24:MI:SS')
,severity
, problem_text
,status
FROM aircom.alarms
WHERE status = 1
AND TO_char (alarm_datetime,'DD.MM.YYYY HH24:MI:SS') > TO_DATE ('07.09.2008 09:43:00', 'DD.MM.YYYY HH24:MI:SS')
ORDER BY ALARM_DATETIME DESC
Just before executing the query:
alter session set NLS_DATE_FORMAT = "DD.MM.YYYY HH24:MI:SS";
or whichever format you are giving the information to the date function. This should fix the ORA error
A simple view like this was giving me the ORA-01861 error when executed from Entity Framework:
create view myview as
select * from x where x.initialDate >= '01FEB2021'
Just did something like this to fix it:
create view myview as
select * from x where x.initialDate >= TO_DATE('2021-02-01', 'YYYY-MM-DD')
I think the problem is EF date configuration is not the same as Oracle's.
If you are using JPA to hibernate make sure the Entity has the correct data type for a field defined against a date column like use java.util.Date instead of String.