Format a DateTime for Oracle (to a String)


Here’s a DateTime extension method that will return an Oracle formatted date string. It’s probably better to let the ADO.Net provider do this for you but there maybe cases where that isn’t desirable or possible (a case like that maybe interacting with a report tool via code).

VB.Net

''' <summary>
''' Returns a string formatted in an Oracle accepted format.
''' </summary>
''' <param name="dateTime"></param>
<System.Runtime.CompilerServices.Extension()> _
Public Function ToOracleSqlDate(dateTime As DateTime) As String
    Return String.Format("to_date('{0}','dd.mm.yyyy hh24.mi.ss')", dateTime.ToString("dd.MM.yyyy HH:mm:ss"))
End Function

C#

public static string ToOracleSqlDate(this DateTime dateTime)
{
    return String.Format("to_date('{0}','dd.mm.yyyy hh24.mi.ss')", dateTime.ToString("dd.MM.yyyy HH:mm:ss"));
}