Skip to Main Content
  • Questions
  • Insert datetime values from .NET apps

Breadcrumb

May 4th

Question and Answer

Connor McDonald

Thanks for the question, Geraldo.

Asked: November 27, 2018 - 5:31 pm UTC

Last updated: November 28, 2018 - 1:35 am UTC

Version: 18.3.0

Viewed 1000+ times

You Asked

Hello, Team.

A .NET app is being developed by our team. We are using Oracle database 18.3.0.

In order to insert date and time in a date column developers use the following code:

TO_DATE(TO_CHAR(:DATE_COLUMN,'DD/MM/RRRR' HH:MI:SS AM), 'DD/MM/RRR' HH:MI:SS AM').

Why do they have to use TO_CHAR in order to insert date and time? Is that correct? What is the best way to insert datetime values into Oracle Database from .NET apps? I thought with TO_DATE(:DATE_COLUMN,'DD/MM/RRR' HH:MI:SS AM') was enough.

Note: We are using spanish date format and because of that we write DD first.

and Connor said...

If you are using Oracle Data Provider for .Net, you would use the date datatype directly, eg

OracleParameter myDateParameter = new OracleParameter();
myDateParameter.OracleDbType = OracleDbType.Date;
myDateParameter.Value = [your .Net datetime value];

Command = new OracleCommand("insert into T values ( :myDateParameter)", connection);


but otherwise, if the incoming value is being presented as a string, then yes, a simple TO_DATE around the value is all that is required.

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library