ORA-01830: date format picture ends before converting entire input string / Select sum where date query - oracle

I am trying to execute my query through Java like this:
public ResultSet execSumStatment2() throws SQLException{
String query = "Select SUM(A) as NCCSeptember from NCC where Datum >= '01-09-2013 00:00:00' and Datum <= '30-09-2013 23:59:59'";
return execStatement(query);
}
Then i call execSumStatement in the class:
sql.execSumStatement2 () ;
When I run it I get the following error message:
java.sql.SQLDataException: ORA-01830: date format picture ends before converting entire input string
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:208)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:886)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1175)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1296)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3657)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1495)
at com.bachelorarbeit.SQLExecutor.execStatement(SQLExecutor.java:20)
at com.bachelorarbeit.SQLExecutor.execSumStatment2(SQLExecutor.java:56)
at com.bachelorarbeit.Test.doGet(Test.java:63)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
When I execute the same query in SQL Developer it works just fine. I already made a search over the internet, and I guess the problem is the datatype of the date in the query, which should be Date, but everything I tried didn't work...

I think you should not rely on the implicit conversion. It is a bad practice.
Instead you should try like this:
datenum >= to_date('11/26/2013','mm/dd/yyyy')
or like
datenum >= date '2013-09-01'

You can try this:
Select To_date ('15/2/2007 00:00:00', 'DD/MM/YYYY HH24:MI:SS'),
To_date ('28/2/2007 10:12', 'DD/MM/YYYY HH24:MI:SS')
From DUAL;
Source: http://notsyncing.org/2008/02/manipulando-fechas-con-horas-en-plsql-y-sql/

In SQL Developer ..Go to Preferences-->NLS-->and change your date format accordingly

What you have written in your sql string is a Timestamp not Date. You must convert it to Date or change type of database field to Timestamp for it to be seen correctly.

You can try as follows it works for me
select * from nm_admission where trunc(entry_timestamp) = to_date('09-SEP-2018','DD-MM-YY');
OR
select * from nm_admission where trunc(entry_timestamp) = '09-SEP-2018';
You can also try using to_char but remember to_char is too expensive
select * from nm_admission where to_char(entry_timestamp) = to_date('09-SEP-2018','DD-MM-YY');
The TRUNC(17-SEP-2018 08:30:11) will give 17-SEP-2018 00:00:00 as a result, you can compare the only date portion independently and time portion will skip.

You can use
Select to_date('08/15/2017 12:00:00 AM','MM/DD/YYYY HH:MI:SS AM') from dual;
If you are using it in an SP then your variable datatype should be Varchar2
and also in your ado.net code the datatype of your input parameter should be
OracleDbType.Varchar2
Basically I had to put a DateFrom and DateTo filter in my SP so I passed dates as String in it.
Note: This is one of the solution which worked for me, there could be more solutions to this problem.

SELECT CAST(COLUMN_NAME AS DATE) FROM TABLE_NAME

Related

java.sql.SQLDataException: ORA-01830: date format picture ends before converting entire input string (Oracle)

I'm trying to run a script through Java code
query.append("SELECT CNAME,CREATEDATE FROM MERGE_DATA_VW \r\n" +
"WHERE TO_DATE(CREATEDATE, 'DD-MM-RR') = TO_DATE('26-NOV-19', 'DD-MM-RR')\";");
stmt = con.prepareStatement(query.toString());
rs = stmt.executeQuery();
When trying to run the script in Oracle SQL Developer it works fine, but when running my java code it gives an SQLDataException:
java.sql.SQLDataException: ORA-01830: date format picture ends before converting entire input string
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:837)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:445)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:523)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:207)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:863)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1153)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1275)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3576)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3620)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1491)
I can't find whats causing the error, please anyone? Thank you
Assuming that CREATEDATE is a column of the DATE data type then you do not need to use TO_DATE on it (as it is already a DATE), you also do not need the trailing \";.
You could also use a date literal.
So assuming that CREATEDATE always has a mignight time component:
query.append("SELECT CNAME,CREATEDATE FROM MERGE_DATA_VW \r\n" +
"WHERE CREATEDATE = DATE '2019-11-26'");
If your CREATEDATE column has a non-midnight time component then you need to compare on a range (or use TRUNC but that would not allow you to use an index on the column and would require a separate function-based index):
query.append("SELECT CNAME,CREATEDATE FROM MERGE_DATA_VW \r\n" +
"WHERE CREATEDATE >= DATE '2019-11-26' AND CREATEDATE < DATE '2019-11-27'");
I don' think you need TO_DATE for CREATEDATE as it is already a date.
Also, your TO_DATE('26-NOV-19', 'DD-MM-RR') should be TO_DATE('26-NOV-19', 'DD-MON-RR')
Try this:
SELECT
CNAME,
CREATEDATE
FROM
MERGE_DATA_VW
WHERE
TRUNC(CREATEDATE) = TO_DATE('26-NOV-19', 'DD-MON-RR');
Cheers!!
Wrong date format
TO_DATE('26-NOV-19', 'DD-MM-RR')
Please user Correct date
ie 'DD-MON-RR' FOR '26-NOV-19'
You where caluse should look like -
WHERE TO_DATE(CREATEDATE, 'DD-MM-RR') = TO_DATE('26-11-19', 'DD-MM-RR')

Can varchar datatype be a timestamp in Confluent?

I'm using confluent to implement realtime ETL.
My datasource is oracle, every table has a column named ts ,it's data type is varchar, but data in this column is YYYY-MM--DD HH24:MI:SS format.
can I use this column as timestamp in confluent kafka connector ?
how to config the xxxxx.properties file?
mode=timestamp
query= select to_date(a.ts,'yyyy-mm-dd hh24:mi:ss') tsinc,a.* from TEST_CORP a
poll.interval.ms=1000
timestamp.column.name=tsinc
connector.class=io.confluent.connect.jdbc.JdbcSourceConnector
query=select * from NFSN.BD_CORP
mode=timestamp
poll.interval.ms=3000
timestamp.column.name=TS topic.prefix=t_ validate.non.null=false
then I get this error:
[2018-12-25 14:39:59,756] INFO After filtering the tables are:
(io.confluent.connect.jdbc.source.TableMonitorThread:175) [2018-12-25
14:40:01,383] DEBUG Checking for next block of results from
TimestampIncrementingTableQuerier{table=null, query='select * from
NFSN.BD_CORP', topicPrefix='t_', incrementingColumn='',
timestampColumns=[TS]}
(io.confluent.connect.jdbc.source.JdbcSourceTask:291) [2018-12-25
14:40:01,386] DEBUG TimestampIncrementingTableQuerier{table=null,
query='select * from NFSN.BD_CORP', topicPrefix='t_',
incrementingColumn='', timestampColumns=[TS]} prepared SQL query:
select * from NFSN.BD_CORP WHERE "TS" > ? AND "TS" < ? ORDER BY "TS"
ASC
(io.confluent.connect.jdbc.source.TimestampIncrementingTableQuerier:161)
[2018-12-25 14:40:01,386] DEBUG executing query select
CURRENT_TIMESTAMP from dual to get current time from database
(io.confluent.connect.jdbc.dialect.OracleDatabaseDialect:462)
[2018-12-25 14:40:01,388] DEBUG Executing prepared statement with
timestamp value = 1970-01-01 00:00:00.000 end time = 2018-12-25
06:40:43.828
(io.confluent.connect.jdbc.source.TimestampIncrementingCriteria:162)
[2018-12-25 14:40:01,389] ERROR Failed to run query for table
TimestampIncrementingTableQuerier{table=null, query='select * from
NFSN.BD_CORP', topicPrefix='t_', incrementingColumn='',
timestampColumns=[TS]}: {}
(io.confluent.connect.jdbc.source.JdbcSourceTask:314)
java.sql.SQLDataException: ORA-01843: not a valid month
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:208)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:886)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1175)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1296)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3657)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1495)
at io.confluent.connect.jdbc.source.TimestampIncrementingTableQuerier.executeQuery(TimestampIncrementingTableQuerier.java:168)
at io.confluent.connect.jdbc.source.TableQuerier.maybeStartQuery(TableQuerier.java:88)
at io.confluent.connect.jdbc.source.TimestampIncrementingTableQuerier.maybeStartQuery(TimestampIncrementingTableQuerier.java:60)
at io.confluent.connect.jdbc.source.JdbcSourceTask.poll(JdbcSourceTask.java:292)
at org.apache.kafka.connect.runtime.WorkerSourceTask.poll(WorkerSourceTask.java:244)
at org.apache.kafka.connect.runtime.WorkerSourceTask.execute(WorkerSourceTask.java:220)
at org.apache.kafka.connect.runtime.WorkerTask.doRun(WorkerTask.java:175)
at org.apache.kafka.connect.runtime.WorkerTask.run(WorkerTask.java:219)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748) [2018-12-25 14:40:01,390] DEBUG Resetting querier
TimestampIncrementingTableQuerier{table=null, query='select * from
NFSN.BD_CORP', topicPrefix='t_', incrementingColumn='',
timestampColumns=[TS]}
(io.confluent.connect.jdbc.source.JdbcSourceTask:332) ^C[2018-12-25
14:40:03,826] INFO Kafka Connect stopping
(org.apache.kafka.connect.runtime.Connect:65) [2018-12-25
14:40:03,827] INFO Stopping REST server
(org.apache.kafka.connect.runtime.rest.RestServer:223)

Creating a function that returns the day of the week for a specified date

I'm trying to create a function that returns the day of the week for a specified date, and I tried the one that's on the bottom, but unsuccessful.
SELECT DAYOFWEEK(‘2015-07-04’);
Use TO_CHAR for that.
select
to_char(date'2015-07-04', 'Day'),
to_char(date'2015-07-04', 'Day', 'NLS_DATE_LANGUAGE=american'),
to_char(date'2015-07-04', 'Day', 'NLS_DATE_LANGUAGE=german'),
to_char(date'2015-07-04', 'DY'),
to_char(date'2015-07-04', 'D') -- result depends on NLS_TERRITORY which you cannot specify here unfortunately
from dual;
The string delimiter in SQL is ', but I guess that was just a copy & paste error? A date literal in Oracle starts with DATE. To select a single value in Oracle, select from DUAL. You can optionally specify a language when you want to see weekday names. As to the day number, this depends on a session setting, unfortunately, with 1 being either Sunday or Monday.
Try:
select to_char(to_date('2015-07-04','yyyy-dd-mm'), 'DAY')
I think it should work.
Referred from:
How to get the week day name from a date?

how to display date and time from oracle database jee application

I'm trying to display data from database ( two different tables) where two columns are type date. I have a problem with formatting: I need to get date and time but I'm getting date and 00:00:00 for time. Does any one know how to use to_char: I tried this but it doesn't work (query laid out for readability in SO):
query.append("SELECT CONCAT(ins.id,'_'
,com.compte,'_'
,com.cin,'_'
,com.nomAff,'_'
,ins.to_char(dateDemande,'DD/MM/YYYY HH:MI:SS ') ,'_'
,ins.typeTPEdemande ,'_'
,ins.statut,'_'
,ins.to_char(dateStatut,'DD/MM/YYYY HH:MI:SS '))
FROM InstallationTPE ins , Commercant com
WHERE com.compte=ins.compte
AND ins.statut = ?");
Thank you
This is wrong: ins.to_char(dateDemande,'DD/MM/YYYY HH:MI:SS '). The TO_CHAR call must wrap the entire name, including the table alias. So do this:
to_char(ins.dateDemande,'DD/MM/YYYY HH:MI:SS ')
Same for dateStatut.

Using Oracle to_date function for date string with milliseconds

I have to perform some inserts into an Oracle DB. I have some dates
in the following format
'23.12.2011 13:01:001'
Following the documentation I wrote inserts to_date as follows:
to_date('23.12.2011 13:01:01', 'DD.MM.YYYY HH24:MI:SS')
which works properly. Now I have dates with milliseconds with the format
'23.12.2011 13:01:001'
I've tried the following:
to_date('23.12.2011 13:01:001', 'DD.MM.YYYY HH24:MI:SSFF3')
which is incorrect (delivers an error 01821. 00000 - "date format not recognized").
Which "String" should I use for this format with milliseconds?
An Oracle DATE does not store times with more precision than a second. You cannot store millisecond precision data in a DATE column.
Your two options are to either truncate the string of the milliseconds before converting it into a DATE, i.e.
to_date( substr('23.12.2011 13:01:001', 1, 19), 'DD.MM.YYYY HH24:MI:SS' )
or to convert the string into a TIMESTAMP that does support millisecond precision
to_timestamp( '23.12.2011 13:01:001', 'DD.MM.YYYY HH24:MI:SSFF3' )
TO_DATE supports conversion to DATE datatype, which doesn't support milliseconds. If you want millisecond support in Oracle, you should look at TIMESTAMP datatype and TO_TIMESTAMP function.
Hope that helps.
For three digits millisecond:
TO_CHAR(LN_AUTOD_UWRG_DTTM,'MM/DD/YYYY HH24:MI:SS.FF3')
For six digits millisecond:
TO_CHAR(LN_AUTOD_UWRG_DTTM,'MM/DD/YYYY HH24:MI:SS.FF'),
You can try this format SS.FF for milliseconds:
to_timestamp(table_1.date_col,'DD-Mon-RR HH24:MI:SS.FF')
For more details:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions193.htm
You have to change date class to timestamp.
String s=df.format(c.getTime());
java.util.Date parsedUtilDate = df.parse(s);
java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedUtilDate.getTime());

Resources