I am using the AS400 platform and a novice with SQL. I have a common field in multiple files - some files define the field as numeric and some alpha. My question: Is there a way in SQL to first convert the numeric field to alpha and then expand the alpha field adding two leading zeros? If this is possible, could it be done on the "live" files to avoid having to delete and then rebuild associated logicals?
This is a project with a lot of files to update and currently we are using the CHGPF command to expand the field and then using a COBOL program to either convert the numeric to alpha or move leading zeros to the existing alpha fields. Any ideas would be appreciated. Thanks.
Software/Hardware used:
as400
ASKED:
October 9, 2009 4:37 PM
UPDATED:
October 12, 2009 11:45 PM
If you want to alter the database structure, it can’t be done directly against the current tables. SQL will block attempts to alter numeric columns to character and vice versa.
But, a SQL VIEW can present numeric columns as character and vice versa. This could let you create VIEWs that present all of the columns in a common format. Once you are satisfied with the format, you can create new tables with the desired format and populate it with the data from the original tables.
Existing programming probably has programming that does these conversions. That programming would need to be changed to honor the new formats.
Other conversions can be done against live tables when they data types allow it. For example, you change a CHAR(8) column to CHAR(10) or you can change a CHAR() to a VARCHAR(). You can’t change both length and datatype in a single operation; it would take two ALTERs to go from CHAR(10) to VARCHAR(20) for example.
Another way to CAST a 4-digit NUMERIC to CHAR(4) with leading zeroes:
CAST( RIGHT('0000' CONCAT VARCHAR(numeric_col),4)) AS CHAR(4)) AS NewChar4Tom