I've used SQL Server for years but I'm new to Oracle SQL. I'm trying to refactor an old application and one of the things I'm doing is moving it to the newer Oracle Managed Data Access. The application uses all in-line SQL with parameters.
I'm having an issue with the a query that deals with dates. I know Oracle expects dates to be in a specific format but I thought if I used the TO_DATE function I could work with whatever format I use.
When I try to run the following query with parameters I'm getting an error
One of the identified items was in an invalid format
I'm assuming it's with the date piece.
If I use LinqPad and run the following query it will update just fine.
update SAP_DATES
set DATE_VALUE = TO_DATE('2010-03-26','yyyy-mm-dd'),
DATE_MODIFIED = TO_DATE('2015-06-12','yyyy-mm-dd')
where
EMP = '00000197' and DATE_TYPE = 'Retirement Date '
However, if I use the following I get the following I get the error. I can confirm the date values right before calling the SQL being in a 'yyyy-mm-dd' format. No hours or minutes.
public const String UPDATE_DATE_RECORD_SQL = "update SAP_DATES set " +
"DATE_VALUE = TO_DATE(:dtValue,'yyyy-mm-dd'), DATE_MODIFIED = TO_DATE(:dtModified,'yyyy-mm-dd') " +
"where EMP = :Emp and DATE_TYPE = :dtType";
and in the data layer
using (objConnection)
{
OracleCommand objCmd = objConnection.CreateCommand();
objCmd.CommandText = oracleCmdText;
objCmd.Parameters.Add(":Emp", OracleDbType.Varchar2).Value = employeeId;
objCmd.Parameters.Add(":dtType", OracleDbType.Varchar2).Value = dtType;
objCmd.Parameters.Add(":dtValue", OracleDbType.Date).Value = dtValue;
objCmd.Parameters.Add(":dtModified", OracleDbType.Date).Value = dtModified;
objConnection.Open();
objCmd.ExecuteNonQuery();
}
Anyone know why I can run the query using LinqPad but if I use the in-line SQL with parameters I get the error?
:dtValueand:dtModifiedshould be of typeVarchar2becauseTO_DATEis expecting a string in the specified format for conversion into a date.