VB.NET OLEDBDataReader - Null value in resultset (DATETIME) - oracle

I'm new to VB.NET programming, so pointing to examples is greatly appreciated.
I've got a OleDB connection to an Oracle 11g database. I'm reading a single row from a table and trying to populate a row object (mClockInOutInfo). The problem I'm running into is that there is a null DateTime (timestamp) column in the returned row.
Of course, I started with the mClockInOutInfo.RowDate = dr.GetDateTime(0) (assume the RowDate column is the first column in the query).
I quickly found out that nulls should be checked, so I added the = If(IsDBNull(dr.GetDateTime(0)), DBNull.value, dr.getDateTime(0)) to the code. (I now use the NullSafeString functions found around here somewhere. But there was nothing for DateTime types. I tried to make one, no success. So I'm still with the If(IsDbNull(...)) code for my datetime columns.
The problem I'm having is that I get the error: "Specified cast is not valid." when evaluating that statement. I've verified the column type with the .GetDataTypeName(0) method. It returns DBTYPE_DBTIMESTAMP. I've reviewed the schema information to verify that the column is actually the information I'm looking for. From what I can tell, the error is being generated while evaluating the IsDBNull(drRecent.GetDateTime(0)) portion of the if() statement. I've confirmed this by breaking the statement into a multi-line if statement:
if IsDBNull(dr.GetDateTime(0)) Then
...
Else
...
End If
I get the same cast error thrown by the if IsDBNull(...) line.
So, I went back to the SQL string and did a Coalesce on the column with the empty data in it. I received the same cast error. Finally, frustrated, I changed the sql query to use the TO_DATE function and fed in a date string with the appropriate format parameter. I STILL receive this same cast error.
The property in the mClockInOutInfo is defined as DateTime. There are other columns defined exactly the same way and those that have data in the table are not giving any errors.
Any ideas of what I should be doing when the database allows nulls in a DateTime (Timestamp) column? All ideas welcomed. Thank you in advance.

Related

JDBC Error in insert with DB2 (works with Sql Server)

I use in a Java Application JDBC to query the DBMS. The application works correctly with Sql Server but I get this error in DB2 during one insert:
com.ibm.db2.jcc.am.SqlDataException: DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=1, DRIVER=3.63.75
The insert is made using the ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE.
My query is a plain select of the table, then I declare my PreparedStatement, passing the parameters and afterwards with the ResultSet I do first the moveToInsertRow() and then the insertRow().
Do you know if there are any problems with this approach using DB2?
As I told you before the same code works correctly with Sql Server.
SQL Code -302 on DB2 means:
THE VALUE OF INPUT VARIABLE OR PARAMETER NUMBER position-number IS INVALID OR TOO LARGE FOR THE TARGET COLUMN OR THE TARGET VALUE
So it seems like you are trying to insert a value into a column which is too large or too short (e.g. Hello World into a varchar(5)). Probably the column has a different length in DB2 and sql-server or you are inserting different values.
Probably too late to add to this thread.. but someone else might find it useful
Got the same SQL Exception when trying to do a SELECT : didn't realize the property value in WHERE clause was exceeding the limit on the corresponding column
SELECT * FROM <schema>.<table_name> WHERE PropertyName = 'value';
value was a VARCHAR type but exceeded the Length limit
Detailed exception does say it clearly that data integrity was violated: org.springframework.dao.DataIntegrityViolationException
So a good idea would be to do a length check on the value(s) that are being set on the properties before firing any queries to the database.

timestamp not working in hive

I have a table with one column data type as 'timestamp'. Whenever i try to do some queries on the table, even simple select statement i am getting errors.
example of a row in my column,
'2014-01-01 05:05:20.664592-08'
The statement i am trying,
'select * from mytable limit 10;'
the error i am getting is
'Failed with exception java.io.IOException:java.lang.NumberFormatException: For input string: "051-08000"'
Date functions in hive like TO_DATE are also not working.If i change the data type to string, i am able to extract the date part using substring. But i need to work with timestamp.
Has anyone faced this error before? Please let me know.
Hadoop is having trouble understanding '2014-01-01 05:05:20.664592-08' as a timestamp because of the "592-08" at the end. You should change the datatype to string, cut off the offending portion with a string function, then cast back to timestamp:
select cast(substring(time_stamp_field,1,23) as timestamp) from mytable

ODP.Net OracleBulkCopy 'not a valid month' with a date field

First it is important to note that this error is occuring while importing a set of data using the OracleBulkCopy object in a WPF application with the data stored in a DataTable object.
In this table there is one column which is of type "Date" (oracle) and the corresponding type is DateTime? (nullable) in the WPF application. However when setting up the DataTable for the OracleBulkCopy object I'm setting the data type of that column to DateTime (non-nullable) as nullable types are not permitted in the DataTable's DataColumn objects.
What is happeneing is that the data import works fine when all date values are null but as soon as I try an import with any actual dates I'm getting the error "Not a valid month" from the Oracle server. Normally when something like this happens you can use "to_date" to specify how the date is supposed to be interpreted.
Using OracleBulkCopy this is not possible and should also not be necessary as the dates aren't strings but actual DateTime objects.
I have also tried using the data type Oracle.DataAccess.Types.OracleDate but this had the same result.
I have not been able to find any solution to this so any help would be appreciated.

Crystal Reports Issue turning a string into a number

I'm having one of those throw the computer out the window days.
I am working on a problem involving Crystal Reports (Version 10) and an Oracle Database (11g).
I am taking a view from the database that returns a string (varcahr2(50)) which is actually a number, when a basic SELECT * query is run on this view I get the number back in the format 000000000000100.00.
When this view is then used in Crystal Reports I can view the field data, but I can't sum the data as it is not a number.
I began, by attempting to using ToNumber on the field, to which Crystal's response was that the string was not numeric text. Ok fair enough, I went back to the view and ran TO_NUMBER, when this was then used in crystal it did not return any results. I also attempted to run TO_CHAR on the view so that I could hopefully import the field as text and then perform a ToNumber, yet the same as with the TO_NUMBER no records were displayed.
I've started new reports, I've started new views. No avail.
This seems to have something to do with how I am retrieving the data for the view.
In simplistic terms I'm pulling data from a table looking at two fields a Foreign Key and a Value field.
SELECT PRIMARY_KEY,
NVL(MAX(DECODE(FOREIGN_KEY, FOREIGN_KEY_OF_VALUE_I_NEED, VALUE_FIELD)), 0)
FROM MY_TABLE
GROUP BY PRIMARY_KEY
When I attempted to put modify the result using TO_NUMBER or TO_CHAR I have used it around the VALUE_FIELD itself and the entire expression, wither way works when the run in a SQL statement. However any TO_NUMBER or TO_CHAR modification to the statement returns no results in Crystal Reports when the view is used.
This whole problem smacks of something that is a tick box or equivalent that I have overlooked.
Any suggestions of how to solve this issue or where I could go to look for an answer would be greatly appreciated.
I ran this query in SQL Developer:
SELECT xxx, to_number(xxx) yyy
FROM (
SELECT '000000000000100.00' XXX FROM DUAL
)
Which resulted in:
XXX YYY
000000000000100.00 100
If your field is truly numeric, you could create a SQL Expression field to do the conversion:
-- {%NUMBER_FIELD}
TO_NUMBER(TABLE.VALUE_FIELD)
This turned out to be an issue with how Crystal Reports deals with queries from a database. All I needed to do was contain my SQL statement within another Select Statement and on this instance of the column apply the TO_NUMBER so that Crystal Reports would recognize the column values as numbers.
Hopefully this helps someone out, as this was a terrible waste of an afternoon.

Getting java.sql.SQLException: Invalid column index while inserting into table

I have been working with a snippet of code where java.sql.SQLException: Invalid column index is being thrown at runtime.
I have worked several times in past with this exception and every time i find that developer has used wrong indexes while using rs.getsString() method.
But my problem this time is that i am getting this exception while inserting the records into the table. Can any one please suggest what is the reason why i am getting this exception while inserting into the database?
Also i am not able to see the query being executed at the time of exception because the driver being used (oracle drivers) don't print the query when i try to use prepareStmt.toString(). This method print the object reference instead.
Please help. Any help is highly appreciated.
Best Regards
Anubhav Jain!
I'm guessing the problem is you do setXXXX to a prepared statement with wrong index (for example 0 or bigger than the number of '?' in the SQL statement)
I also faced the same issue. Later I found out that in my query string I had used quote marks around ? symbol, which is not allowed.
For example :
insert into document_table(ID, SOURCE, DESTINATION, STATUS) values('?','Active','Backup' ,'D')
is not allowed. It should have been written as:
insert into document_table(ID, SOURCE, DESTINATION, STATUS) values(?,'Active','Backup' ,'D')

Resources