You can use the DATE function to convert a date in YYYYMMDD format to YYYY-MM-DD format in DB2. Here's an example code:

SELECT DATE(SUBSTR(date_column, 1, 4) || '-' || SUBSTR(date_column, 5, 2) || '-' || SUBSTR(date_column, 7, 2)) FROM table_name;

Where:

  • date_column is the column containing the original date in YYYYMMDD format.
  • table_name is the name of your table.

This code utilizes the SUBSTR function to split the YYYYMMDD date into year, month, and day components. Then, it uses the || operator to concatenate them back together into a YYYY-MM-DD format string. Finally, the DATE function converts the concatenated string into a date data type.

After executing the code, the query will return the date results in YYYY-MM-DD format.

DB2 SQL: Convert Date Format from YYYYMMDD to YYYY-MM-DD

原文地址: https://www.cveoy.top/t/topic/nEvL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录