Format a DateTime for Oracle (to a String)

Posted by Blake on 8/13/2012

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).


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


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