SQL Functions for Mashing Characters
August 14, 2002 Timothy Prickett Morgan
Hey, Howard:
I have a field that has embedded ‘-‘ characters that I want to remove.
|
For example, if the field contains 01-111-3345, I want to return it as 001113345.
However, the translate function does not accept an empty string.
Can you tell me how to remove characters from a string in SQL?
— Mike
Unfortunately, there is not an easy way to do this when you have more than one occurrence of the character in a string.
However, a user-defined function using the Persistent Stored Modules (PSM), feature of DB2 is one way to get this done:
CREATE function sqlbook.ELIMCHAR (InString varchar(100),RepChar VARCHAR(1)) returns varchar(100) LANGUAGE SQL BEGIN DECLARE OutStr varchar(100); DECLARE Spos INTEGER; SET OutStr=InString; SET spos = -1; WHILE spos<>0 DO SET Spos = locate(RepChar,OutStr,1); IF Spos>1 AND spos<LENGTH(outstr)-1 THEN SET OutStr = substr(OutStr,1,Spos-1)|| substr(OutStr,Spos+1,LENGTH(OutStr)-spos); ELSEIF Spos=LENGTH(outstr) THEN SET OutStr = substr(OutStr,1,spos-1); ELSEIF spos=1 THEN SET OutStr = substr(OutStr,2,LENGTH(outstr)-1); END IF; END WHILE; RETURN OutStr; END
This user-defined function uses the LOCATE function to find the occurrence of the string. If LOCATE returns 0, none of the conditions are executed and the loop exits. If LOCATE returns a position, first I determine if the position is greater than (>) one and less than (<) the length of the string, which indicates the character is somewhere in the middle of the string. If so, I use the returned position of the character as an argument to the substring function, which returns beginning and ending strings. Subsequently, the CONCAT operator (||) is used to meld the substrings together.
If the position of the passed character is at the end of the string, I use the SUBSTR function to return the string minus the ending character. Finally, if the string is in the first position, I use SUBSTR to remove just the first character. The function loops until all occurrences of the character are replaced.
Here’s one thing to note about this function: I used the VARCHAR data type for both the input string, (InString), the matching character, (RepString), and the return data type, (OutStr). This is because all CHAR data types can be promoted to VARCHAR automatically by SQL, but VARCHAR data types cannot be demoted to CHAR. Any strings you pass to the function will be seen as VARCHAR, so if the arguments are declared as CHAR, the function will not be found. Remember, SQL looks for the user-defined functions not only by name, but also ensures that the data types for the arguments match or are compatible.
Here’s an example of how this could affect you. Say I coded the function in the following manner:
CREATE function sqlbook.ELIMSTRING (InString varchar(100), RepChar CHAR(1)) returns varchar(100) ...... more function stuff
If I have a table called XYZ that has a column called ABCD, which is type CHAR(20), the following call will NOT work:
SELECT ELIMSTRING(ABCD,'-') FROM XYZ
This call will fail and indicate that the function ELIMSTRING is not found. It does not fail because column ABCD is a CHAR(20) and the function is coded to accept only VARCHAR(100) arguments in the first position. It will fail because the second argument, ‘-‘, is seen by DB2 as a VARCHAR data type and the function is declared to accept only CHAR(1) data types in the second argument. However, if I code the function with the following declaration it will work:
CREATE function sqlbook.ELIMSTRING (InString varchar(100), RepChar VARCHAR(1)) returns varchar(100) ...... more function stuff
DB2 knows that a CHAR(20) column is compatible with a VARCHAR(100) column, so that passes the data-type test. Since DB2 sees the second argument as a VARCHAR(1) anyway, the second argument also passes the test and the proper function is “found”. If you think about it, it is kind of stupid to declare an argument as VARCHAR(1), but this is required if you want to pass your arguments as literals because DB2 will see the literal string as a VARCHAR, not a CHAR of the length of the literal.
I hope this helps.
— Howard
Howard F. Arner, Jr. is Vice President of Client Server Development, Inc., in Jacksonville, Florida, and he is the author of “iSeries and AS/400 SQL at Work.” You can buy Howard’s book at www.sqlthing.com/books, or go there to get more information about DB2 on the AS/400.
Sponsored By COMMON |
REGISTER FOR COMMON IN DENVER, OCT. 13-17 Get the IT training you need by attending COMMON Users Group’s Fall 2002 IT Education Conference & Expo, October 13-17 in Denver. Early Bird registration is $1,150 until September 4. Choose from over 720 sessions and labs covering a wide range of industry topics. Also receive training from J.D. Edwards, MAPICS, and other vendors. Don’t miss out! Go to www.common.org |