DB2 need a function to return the return the current date in format yyyymmdd

hi,

I need to write a function which when called will return the current date in the format yyyymmdd

thanks
LVL 1
hraja77Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Cenjoy100Commented:
SELECT CHAR(CURRENT_DATE,ISO)
      ,CHAR(CURRENT_DATE,USA)
      ,CHAR(CURRENT_DATE,EUR)
      ,CHAR(CURRENT_DATE,JIS)
FROM SYSIBM.SYSDUMMY1

---Hope Helpful for you..ISO is your required format I guess.
0
hraja77Author Commented:
thanks for that - how can i wrap this and create a function for the first date to be returned ?
what would the function be like and how do i call this - i could create another question for this if you want

thanks
0
Cenjoy100Commented:
Or this will defiantly work for now and in future
---Create and Use following UDF ....ENJOY :)

create function ts_fmt(TS timestamp, fmt varchar(20))
returns varchar(50)
return
with tmp (dd,mm,yyyy,hh,mi,ss,nnnnnn) as
(
    select
    substr( digits (day(TS)),9),
    substr( digits (month(TS)),9) ,
    rtrim(char(year(TS))) ,
    substr( digits (hour(TS)),9),
    substr( digits (minute(TS)),9),
    substr( digits (second(TS)),9),
    rtrim(char(microsecond(TS)))
    from sysibm.sysdummy1
    )
select
case fmt
    when 'yyyymmdd'
        then yyyy || mm || dd
    when 'mm/dd/yyyy'
        then mm || '/' || dd || '/' || yyyy
    when 'yyyy/dd/mm hh:mi:ss'
        then yyyy || '/' || mm || '/' || dd || ' ' ||  
               hh || ':' || mi || ':' || ss
    when 'nnnnnn'
        then nnnnnn
    else
        'date format ' || coalesce(fmt,' <null> ') ||  
        ' not recognized.'
    end
from tmp
-----------------------------------------------------------------------
--Examples

values ts_fmt(current timestamp,'yyyymmdd')
 '20030818'
values ts_fmt(current timestamp,'asa')  
 'date format asa not recognized.'
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

hraja77Author Commented:
sorry i'm new to db2 - how do i now run the function from db2 command line
0
hraja77Author Commented:
i will raise another call for this part as i blieve you deserve more points
0
Cenjoy100Commented:
Ok,Fine once you created that I will put the comments for the same.
But its not difficult.
Trust me you will get it in flash :)
0
hraja77Author Commented:
thanks
0
Cenjoy100Commented:
Or best way you can go through the following link..
http://www.ibm.com/developerworks/data/library/techarticle/adamache/0109adamache.html

as it will be quite difficult to explain each and everything and it might stop your work for no reason.

TC
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB2

From novice to tech pro — start learning today.