ORA-01861: literal does not match format string - sql

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.

Related

How I am able to use To_date function in oracle with date format?

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.

PL SQL - Convert timestamp to datetime/date

select
to_timestamp(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS.FF') as SCHEDULED_TIME,
TRUNC(to_date(to_timestamp(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS.FF'),'YYYY-MM-DD HH24:MI:SS'))
from S_TIDAL_STATUS
The error was:
ORA-01830: date format picture ends before converting entire input string
01830. 00000 - "date format picture ends before converting entire input string"
The goal is to return something like
2017-07-91 23:14:00
(without the content after the dot).
Here's what the SCHEDULED_TIME (timestamp) looked like:
The problem in your attempt is the function TO_DATE() applied to a timestamp. TO_DATE() takes a VARCHAR2 (string) input, not a timestamp. So Oracle converts the timestamp to a string first, implicitly, using your NLS_TIMESTAMP_FORMAT parameter, and then attempts to convert this string to a date. Depending on your NLS_TIMESTAMP_FORMAT, you may get different errors.
The way to convert a timestamp to a date (datetime) - while truncating off the fractions of a second - is with the CAST function. Example:
select systimestamp,
cast (systimestamp as date) as ts_cast_to_date
from dual
;
Alternatively, if all your strings are EXACTLY in that format, you can truncate the strings first and apply TO_DATE directly:
to_date(substr(scheduled_time, 1, 19), 'yyyy-mm-dd hh24:mi:ss')
This should do the trick:
select
to_char(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS.FF') as time_to_csecs,
to_char(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS') as time_to_secs,
TRUNC(to_date(to_char(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')) as time_to_day
from S_TIDAL_STATUS
Please review the docs to see the difference between to_timestamp and to_char.

Convert string to date in Oracle SQL

I'm trying to convert string column to date in Oracle SQL.
Here is my an example of value from the data:
'03/12/14 11:00:00'
Here is my query:
select to_date(A,'DD/MM/YY HH24:MI:SS')
from MyTable
Here is an example of the output:
03-DEC-14
Why my query return only the date but not the hour?
Assuming you are using SQL*Plus (or SQL Developer) the default NLS_DATE_FORMAT is applied when a DATE value is displayed. To verify your current format you can run:
select value
from nls_session_parameters
where parameter = 'NLS_DATE_FORMAT';
To adjust this, run:
alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
Then you should see the time as well.
You are trying to DISPLAY the datetime value. Use TO_CHAR along with proper FORMAT MODEL.
For example,
select to_char(sysdate, 'mm/dd/yyyy hh24:mi:ss') from dual;
A DATE consists of both date and time portions. You just need to make sure you use appropriate format model to display it. Else, you end up with a format which depends on your locale-specific NLS settings.
You can use timestamp
select to_timestamp(A, 'DD/MM/YY HH24:MI:SS') from MyTable
If you want a query that returns the time portion of the date without having to alter the nls_date_format you can convert the date you got from the to_date function to a text representation (the default text representation is dd-Mon-yy as you found out) like this:
select to_char(
to_date(A,'DD/MM/YY HH24:MI:SS'),
''DD/MM/YY HH24:MI:SS')
from MyTable;
Ideally, column A of MyTable should have been defined as a date column instead of a varchar2 column. There are several reasons to do that; data integrity, and better SQL optimization.

Using toChar to output a date in W3C XML Schema xs:dateTime type format

I'm trying to convert sysdate using toChar to the following format:
2006-11-20T17:10:02+01:00
From this format:
16/08/2012 13:40:59
Is there a standard way of doing this?
I've tried using the toChar to specific the T part as a string but it doesn't appear to be working.
Thanks in advance
Jezzipin
EDIT:
I've tried Nicholas' solution however as I mention above, I need to use sysdate. I've used the following select query:
select to_char(to_timestamp_tz(sysdate-365, 'dd/mm/yyyy hh24:mi:ss'),'yyyy-mm-dd"T"hh24:mi:ss TZH:TZM') from dual;
However, this returns:
0012-08-16T00:00:00 +01:00
which is incorrect as it should be 2012-08-16T00:00:00 +01:00
Try:
select to_char(sysdate, 'yyyy-mm-dd') || 'T' || to_char(sysdate,'hh24:mi:ss') || sessiontimezone
from dual;
Returns:
2013-08-16T13:00:51+00:00
To display sysdate in the format that contains timezone information you need to do a series of conversions:
Convert sysdate to string literal using to_char() function.
Convert string literal to timestamp with tome zone using to_timestamp_tz() function.
And finally, convert the final result back to string literal using to_char().
as follows:
select to_char(
to_timestamp_tz(
to_char(sysdate - 365, 'dd/mm/yyyy hh24:mi:ss')
, 'dd/mm/yyyy hh24:mi:ss')
, 'yyyy-mm-dd"T"hh24:mi:ss TZH:TZM'
) as res
from dual
Result:
RES
--------------------------
2012-08-16T17:29:28 +04:00
You can include string literal in the format mask enclosing it with double quotes.
The value with 'T' is called ISO 8601, also known as 'XS:DateTime' or 'XSD:DateTime' or 'XML Schema DateTime'.
Note that Oracle's sessiontimezone command can return not only '-04:00' but also a value like 'America/Los_Angeles' (depending on the db settings), which is probably not something you want.
There is also a replacement required since TZ_OFFSET returns \0 - terminated string. So this should work:
create or replace function to_iso8601 (datetime_in in date) return varchar is
begin
return to_char(datetime_in, 'yyyy-mm-dd') || 'T' || to_char(datetime_in,'hh24:mi:ss') || replace(TZ_OFFSET(DBTIMEZONE),chr(0));
end;
/
select to_iso8601(sysdate) from dual;
result:
2014-11-03T16:53:45-04:00

How to change the date format from MM/DD/YYYY to YYYY-MM-DD in PL/SQL?

I have a date column in a table stored as MM/DD/YYYY format. I have to select and store the same date in another table in YYYY-MM-DD format i.e. XSD Date Format. But I am not able to do it. I am using this query:
select to_date(date_column,'YYYY-MM-DD') from table;
But still I am not able to do it. Giving me error
ORA-01843 : not a valid month
use
select to_char(date_column,'YYYY-MM-DD') from table;
It sounds like you've got it the wrong way round. If your existing data is in MM/DD/YYYY format, then you want:
select to_date(date_column,'MM/DD/YYYY') from table;
to convert the existing data to DATE values. (I do wonder why they're not stored as dates, to be honest...)
If you want to perform the conversion in one step, you might want:
select to_char(to_date(date_column,'MM/DD/YYYY'), 'YYYY-MM-DD') from table;
In other words, for each row, parse it in MM/DD/YYYY format, then reformat it to YYYY-MM-DD format.
(I'd still suggest trying to keep data in its "natural" type though, rather than storing it as text in the first place.)
I assume that you can use the Oracle SQL Developer, which you can download from here.
You can define the date format which you want to work with:
ALTER SESSION SET nls_date_format='yyyy-mm-dd';
With this, now you can perform a query like this:
SELECT * FROM emp_company WHERE JDate = '2014-02-25'
If you want to be more specific you can define the date format like this:
ALTER SESSION SET nls_date_format='yyyy-mm-dd hh24:mi:ss';
To convert a DATE column to another format, just use TO_CHAR() with the desired format, then convert it back to a DATE type:
SELECT TO_DATE(TO_CHAR(date_column, 'DD-MM-YYYY'), 'DD-MM-YYYY') from my_table
select to_date(to_char(ORDER_DATE,'YYYY/MM/DD'))
from ORDERS;
This might help but, at the end you will get a string not the date. Apparently,
your format problem will get solved for sure .
For military time formatting,
select TO_CHAR(SYSDATE, 'yyyy-mm-dd hh24:mm:ss') from DUAL
--2018-07-10 15:07:15
If you want your date to round DOWN to Month, Day, Hour, Minute, you can try
SELECT TO_CHAR( SYSDATE, 'yyyy-mm-dd hh24:mi:ss') "full-date" --2018-07-11 10:40:26
, TO_CHAR( TRUNC(SYSDATE, 'year'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-year"-- 2018-01-01 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'month'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-month" -- 2018-07-01 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'day'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-Sunday" -- 2018-07-08 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'dd'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-day" -- 2018-07-11 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'hh'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-hour" -- 2018-07-11 10:00:00
, TO_CHAR( TRUNC(SYSDATE, 'mi'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-minute" -- 2018-07-11 10:40:00
from DUAL
For formats literals, you can find help in
https://docs.oracle.com/cd/B28359_01/server.111/b28286/functions242.htm#SQLRF52037
You can do this simply by :
select to_char(to_date(date_column, 'MM/DD/YYYY'), 'YYYY-MM-DD') from table
According to the comments, the data-type in the datatable is DATE.
So you should simply use:
"select date_column from table;"
Now if you execute the select you will get back a date data-type, which should be what you need for the .xsd.
Culture-dependent formating of the date should be done in the GUI (most languages have convenient ways to do so), not in the select-statement.
Basically , Data in a Date column in Oracle can be stored in any user defined format or kept as default.
It all depends on NLS parameter.
Current format can be seen by : SELECT SYSDATE FROM DUAL;
If you try to insert a record and insert statement is NOT in THIS format then it will give :
ORA-01843 : not a valid month error.
So first change the database date format before insert statements ( I am assuming you have bulk load of insert statements) and then execute insert script.
Format can be changed by :
ALTER SESSION SET nls_date_format = 'mm/dd/yyyy hh24:mi:ss';
Also You can Change NLS settings from SQL Developer GUI , (Tools > preference> database > NLS)
Ref: http://oracle.ittoolbox.com/groups/technical-functional/oracle-sql-l/how-to-view-current-date-format-1992815
This worked for me! You can convert to datatype you want be it a date or string
to_char(TO_DATE(TO_CHAR(end_date),'MM-DD-YYYY'),'YYYY-MM-DD') AS end_date
Late reply but for.databse-date-type the following line works.
SELECT to_date(t.given_date,'DD/MM/RRRR') response_date FROM Table T
given_date's column type is Date
Just to piggy back off of Yahia, if you have a timestamp you can use this function to cast exclusively as date, removing the timestamps.
TO_CHAR(CAST(DateTimeField AS DATE), 'YYYY-MM-DD') AS TrackerKey__C
Or in my case I need the below format
TO_CHAR(CAST(DateTimeField AS DATE), 'YYYYMMDD') AS TrackerKey__C
SELECT TO_DATE(TO_CHAR(date_column,'MM/DD/YYYY'), 'YYYY-MM-DD')
FROM table;
if you need to change your column output date format just use to_char this well get you a string, not a date.
use
SELECT STR_TO_DATE(date_column,'%Y-%m-%d') from table;
also gothrough
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html