Formatting sysdate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rsrinivasan
    New Member
    • Mar 2007
    • 221

    Formatting sysdate

    Hi,

    When i Execute this query, I got the result as 29-MAY-07
    Code:
    select sysdate from dual;
    But i want 29-MAY-2007.

    What is the query for it?

    Thanks,
  • chandu031
    Recognized Expert New Member
    • Mar 2007
    • 77

    #2
    Originally posted by rsrinivasan
    Hi,

    When i Execute this query, I got the result as 29-MAY-07
    Code:
    select sysdate from dual;
    But i want 29-MAY-2007.

    What is the query for it?

    Thanks,
    Hi,

    It's a pretty straight forward query:
    [code=sql]

    SELECT TO_CHAR(SYSDATE ,'DD-MON-YYYY') FROM DUAL

    [/code]

    Cheers!

    Comment

    • rsrinivasan
      New Member
      • Mar 2007
      • 221

      #3
      Originally posted by chandu031
      Hi,

      It's a pretty straight forward query:
      [code=sql]

      SELECT TO_CHAR(SYSDATE ,'DD-MON-YYYY') FROM DUAL

      [/code]

      Cheers!
      Ok. Now it is working. But i need date type not char type. For that what to do?

      Thanks,
      Srinivas r.

      Comment

      • chandu031
        Recognized Expert New Member
        • Mar 2007
        • 77

        #4
        Originally posted by rsrinivasan
        Ok. Now it is working. But i need date type not char type. For that what to do?

        Thanks,
        Srinivas r.
        Hi,

        You can still use it as a date field. Try inserting to_char(sysdate ,'dd-mon-yyyy')
        into a date field in a table and you will come to know.
        Anyways the other method of changing the date format is to alter the default format in sqlplus:

        [code=sql]

        ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';

        [/code]

        This changed format will hold good for the current session and is generally used during file loads.

        Hope this helped...

        Comment

        • rsrinivasan
          New Member
          • Mar 2007
          • 221

          #5
          Originally posted by chandu031
          Hi,

          You can still use it as a date field. Try inserting to_char(sysdate ,'dd-mon-yyyy')
          into a date field in a table and you will come to know.
          Anyways the other method of changing the date format is to alter the default format in sqlplus:

          [code=sql]

          ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';

          [/code]

          This changed format will hold good for the current session and is generally used during file loads.

          Hope this helped...
          Hi,
          When i am using to_char() function, it converts date type to char type. So im getting error in code behind.
          Then i do not want to set as NLS_DATE_FORMAT = 'DD-MON-YYYY'.
          Because some times i need some other format.

          Plz say any other suggestion.

          Thanks,
          Srinivas r.

          Comment

          • chandu031
            Recognized Expert New Member
            • Mar 2007
            • 77

            #6
            Originally posted by rsrinivasan
            Hi,
            When i am using to_char() function, it converts date type to char type. So im getting error in code behind.
            Then i do not want to set as NLS_DATE_FORMAT = 'DD-MON-YYYY'.
            Because some times i need some other format.

            Plz say any other suggestion.

            Thanks,
            Srinivas r.
            Hi rsrinivasan,

            I guess you are comparing this formatted date with some other date field. If yes then it makes sense to format even that field before doing the actual comparison. For example if say a date variable v_date holds sysdate and you want to compare this then you can do:

            [code=sql]
            SELECT 1 FROM DUAL WHERE TO_CHAR(V,'DD-MON-YYYY') = TO_CHAR(SYSDATE ,'DD-MON-YYYY')
            [/code]

            If your requirement is something else and then please do let me know.

            Cheers!

            Comment

            Working...