The STRING
function parameter is intended only to format the output string to a desired pattern, not to replace chars on it "on the fly". You'll need a CHAR
variable to put the database value on it and you can use the OVERLAY
or SUBSTRING
function to override the first five chars on your string to match your criteria. In your example :
SSNString = '333224444'.
/* Choose the OVERLAY function or SUBSTRING function. You can pass either
a FILL function call, one variable or a fixed string value to OVERLAY
or SUBSTRING function call. They'll behave exactly the same way. */
OVERLAY(SSNString,1,5,'CHAR') = FILL('X',5).
SUBSTRING(SSNString,1,5) = 'XXXXX'.
/* At this point, SSNString is XXXXX4444 */
SSNString = STRING(SSNString,'XXX-XX-9999').
/* Here you'll have XXX-XX-4444 */
MESSAGE SSNString
VIEW-AS ALERT-BOX INFO BUTTONS OK.
In a little more complex and flexible way you can use a function that returns the value formatted as you desire and dismiss the need to use a variable in cases which you want to apply special format to database field values.
FUNCTION formatSSNString RETURNS CHAR
( INPUT pSourceString AS CHAR,
INPUT pFormat AS CHAR,
INPUT pOverlayCount AS INT,
INPUT pStart AS INT,
INPUT pCharOverlay AS CHAR ):
DEF VAR cCharOutput AS CHAR NO-UNDO.
cCharOutput = pSourceString.
OVERLAY(cCharOutput,pStart,pOverlayCount,'CHAR') = FILL(pCharOverlay,pOverlayCount).
cCharOutput = STRING(cCharOutput,pFormat).
RETURN cCharOutput.
END FUNCTION.
DEF VAR SSNString AS CHAR NO-UNDO.
SSNString = '333224444'.
/* This returns XXX-XX-4444 */
MESSAGE formatSSNString(SSNString, 'xxx-xx-9999', 5, 1, 'X')
VIEW-AS ALERT-BOX.
/* This returns 33X-XX-X444 */
MESSAGE formatSSNString(SSNString, 'xxx-xx-9999', 4, 3, 'X')
VIEW-AS ALERT-BOX.
/* This returns 333-XX-4444 */
MESSAGE formatSSNString(SSNString, 'xxx-xx-9999', 2, 4, 'X')
VIEW-AS ALERT-BOX.
/* This returns 333-XX-44YY */
MESSAGE formatSSNString(formatSSNString(SSNString, 'x(9)', 2, 4, 'X'), 'xxx-xx-9999', 2, 8, 'Y')
VIEW-AS ALERT-BOX.
Hope it helps.