Oracle SQL reformat date

Sort:
You are not authorized to post a reply.
Author
Messages
Chesca
Veteran Member
Posts: 490
Veteran Member

    I am writing an SQL statement to select dates from a table and creates a file.  I want to convert date to string mm/dd/yyyy.  Any help would be greatly appreciated.

    select startdate from table; 

    this date is displayed as 31-MAY-15 so I want it displayed as 05-31-2015

    mikeP
    Veteran Member
    Posts: 151
    Veteran Member
      To convert a Lawson date in GL_DATE to string:
      Here's some convert function formats

      CONVERT(CHAR, GL_DATE, 110) mm-dd-yyyy
      CONVERT(CHAR, GL_DATE, 112) yyyyddyy
      CONVERT(CHAR, GL_DATE, 101) mm/dd/yyyy

      The.Sam.Groves
      Veteran Member
      Posts: 89
      Veteran Member
        I'm assuming Oracle SQL due to the fact that your default date format looks like what Oracle uses.

        If so, to control the formatting of a date you would convert it to a character string using the to_char function.

        To convert to the specific format you provided: to_char(date_field,'MM-DD-YYYY')

        So to include this in a SQL statement:

        SELECT 

           TO_CHAR(empl.term_date,'MM-DD-YYYY') as "Term Date" 

         FROM prod.employee empl


        You can learn more concerning the possible date formats from here: https://docs.oracle.com/c.../sql_elements004.htm

        One quirk of Lawson to be aware of is that it doesn't use NULL values in SQL for dates. Instead any empty dates are represented by the date 01/01/1700. So you might want to roll this into a CASE statement to cover that possibility: 


        SELECT 

          CASE 

         WHEN empl.term_date = TO_DATE('01/01/1700','MM/DD/YYYY') 

           THEN '' 

         ELSE 

            TO_CHAR(empl.term_date,'MM-DD-YYYY') 

         END as "Term Date" 

         FROM prod.employee empl

        Chesca
        Veteran Member
        Posts: 490
        Veteran Member
          Exactly what I needed, thank you all for your time and quick response!
          You are not authorized to post a reply.