Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6823211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:45:21+00:00 2026-05-26T21:45:21+00:00

I was trying to make a function to work in db2: CREATE FUNCTION TO_DATE8(DATE_STRING

  • 0

I was trying to make a function to work in db2:

CREATE FUNCTION TO_DATE8(DATE_STRING numeric(8,0))
    RETURNS DATE
    LANGUAGE SQL
    IF DATE_STRING > 0 THEN
         // ERROR ->
         RETURN DATE ( TO_DATE ( SUBSTR ( DATE_STRING , 1 , 8 ) , 'YYYYMMDD' ) )
    ELSE
         RETURN  DATE ( TO_DATE ( '00000000' , 'YYYYMMDD' ) ) 
    END IF
END

ERROR: DATE IS NOT VALID

What to do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:45:22+00:00Added an answer on May 26, 2026 at 9:45 pm

    The form of the procedure required seems to be like this (at least on the iSeries version):

    CREATE FUNCTION TO_DATE8(DATE_STRING numeric(8,0))          
    RETURNS DATE                                            
    LANGUAGE SQL
    BEGIN
        RETURN(CASE WHEN DATE_STRING > 0 THEN DATE(SUBSTR(DATE_STRING, 1, 4) || '-' || 
                                                   SUBSTR(DATE_STRING, 5, 2) || '-' || 
                                                   SUBSTR(DATE_STRING, 7, 2))
                    ELSE DATE('0001-01-01')
                    END);
    END
    

    However:

    • Your procedure is misnamed (reading from a date-8, not to it).
    • Your DATE_STRING is not a string (or even a char), it’s numeric. Please rename it to something that does not include the datatype (dateToConvert works)
    • You seem to want to return something that is not a valid date (all 0s). I’m returning *loval here, although it’s possible it should actually be null.
    • I didn’t put in enough checks for a valid date – this will blow up really easily.
    • If at all possible, the database should be changed to contain actual dates, not a numeric value. Disk is (relative to programmer/architect headaches) cheap.
    • You may also find a calendar file helpful, if the 8-digit numeric was one of the included columns.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a function that returns the content of the webpage
I'm trying to make the add_day function work, but I'm having some trouble. Note
I am trying to make the following recursive function work but keep receiving an
I am trying to make Erlang's Inets mode_esi work and run some function. So,
I'm currently trying to make the function below work correctly. Recently added the IF
I am trying to make a search function to work with algebra problems. I
Im trying to make a function that will return an element of type point:
Im trying to make a function that retrieves a value from the db, but
I'm trying to make a function that holds state but is called with foo().
I'm trying to make a function that will take short hand hex color to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.