We are generating a period dimension in a DB2 (7.2) warehouse using a bunch of auto-generated columns. Many of these columns require a formatted date string. But the DB2 date functions in question return integers, meaning that for a simple result of the form "CCYY-MM-DD", we're using the following:

CAST(YEAR(NATURAL_DATE) AS CHAR(4)) || '-' ||
RIGHT(DIGITS(MONTH(NATURAL_DATE)), 2) || '-' ||
RIGHT(DIGITS(DAY(NATURAL_DATE)), 2)
(1) Is there a shorthand way to do this (say, similar to C's "format" function)?

(2) Is there a way of forcing a result's data type without using CAST?

    Requires Free Membership to View

Looks like you've found the correct way to do this. I have to ask, what data type is the NATURAL_DATE column? If it is a DB2 DATE data type, then you should be able to use CHAR(NATURAL_DATE,ISO) instead. This will return the date as a character expression in the format you require (ISO).

For More Information

This was first published in November 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: