Example of custom DB2 date/time format

Source: Internet
Author: User
Tags db2 date

Custom DB2 date is a common problem. What if your customer wants the date format to be YYYYMMDD? The best method is to write a custom Formatting Function:

The following is an example of customizing the DB2 date function:

 
 
  1. create function ts_fmt(TS timestamp, fmt varchar(20))   
  2. returns varchar(50)   
  3. return   
  4. with tmp (dd,mm,yyyy,hh,mi,ss,nnnnnn) as   
  5. (   
  6. select   
  7. substr( digits (day(TS)),9),   
  8. substr( digits (month(TS)),9) ,   
  9. rtrim(char(year(TS))) ,   
  10. substr( digits (hour(TS)),9),   
  11. substr( digits (minute(TS)),9),   
  12. substr( digits (second(TS)),9),   
  13. rtrim(char(microsecond(TS)))   
  14. from sysibm.sysdummy1   
  15. )   
  16. select   
  17. case fmt   
  18. when 'yyyymmdd'   
  19. then yyyy || mm || dd   
  20. when 'mm/dd/yyyy'   
  21. then mm || '/' || dd || '/' || yyyy   
  22. when 'yyyy/dd/mm hh:mi:ss'   
  23. then yyyy || '/' || mm || '/' || dd || ' ' ||   
  24. hh || ':' || mi || ':' || ss   
  25. when 'nnnnnn'   
  26. then nnnnnn   
  27. else   
  28. 'date format ' || coalesce(fmt,' ') ||   
  29. ' not recognized.'   
  30. end   
  31. from tmp  

This formula is complex at first glance. After a closer look, you will find it easy to use. First, use the Common Table Expression to extract each part of the time format, and then reassemble the output according to the date format provided by the user. This function is flexible. You can simply add the WHEN clause to add the expected date format. When using a function, if the input date format does not exist, the function can also output error information.

Tips for DB2 data deletion

DB2 event monitoring command

How to Implement online DB2 Import

DB2 Online Export Method

Common db2 stored procedure statements

   

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.