To extract month and year from a column in the table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anusha Pesala
    New Member
    • Apr 2013
    • 5

    To extract month and year from a column in the table

    I have a table which has date as (yyyy-mm-dd)'2000-02-04' ,i want to get only month and year.
    i.e the expected result should display 2000,02.
    cqan you help me how to get.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Read http://www.ibm.com/developerworks/da.../0211yip3.html

    Try some SQL and post if you get problems with your SQL.

    Comment

    • Anusha Pesala
      New Member
      • Apr 2013
      • 5

      #3
      I have used Extract function and it is throwing error.
      Error: [SQL0171] Argument 1 of function EXTRACT not valid.
      SQLState: 42815
      ErrorCode: -171

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        What code did you try?

        Comment

        • Anusha Pesala
          New Member
          • Apr 2013
          • 5

          #5
          Code:
          SELECT EXTRACT(MONTH FROM mschof) 
               FROM ccdata.rcmast;
          Last edited by Rabbit; Apr 8 '13, 04:32 PM. Reason: Please use code tags when posting code.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            That's not valid syntax, you probably want
            Code:
            SELECT MONTH(mschof) FROM ccdata.rcmast;

            Comment

            • Anusha Pesala
              New Member
              • Apr 2013
              • 5

              #7
              when i use the above query still i am getting same error

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                What type is the mschof column?

                Comment

                • Anusha Pesala
                  New Member
                  • Apr 2013
                  • 5

                  #9
                  the type of the column is date type

                  Comment

                  • vijay2082
                    New Member
                    • Aug 2009
                    • 112

                    #10
                    Hi,

                    use below query and replace the current date with your column name which is a date data type.

                    C:\>db2 -x "select cast(year(curre nt date) as char(4)) ||','|| cast(month(curr ent date) as char) from sysibm.sysdummy 1 with ur"
                    2013,4

                    C:\>db2 "select cast(year(curre nt date) as char(4)) ||','|| cast(month(curr ent date) as char) from sysibm.sysdummy 1 with ur"

                    1
                    ------
                    2013,4

                    1 record(s) selected.

                    If this doesn't work then paste the below output.

                    db2level

                    db2 connect to <db_name>

                    db2 "describe table <tabschema>.<ta bname>"

                    Cheers, Vijay

                    Comment

                    Working...