Database Forum / DB2 Topics / December 2005
Any function in DB2 to help
|
|
Thread rating:  |
heming_g@hotmail.com - 19 Dec 2005 08:59 GMT to help to Replace the stupid Coding like SUBSTR('0000',LENGTH('22'),4-LENGTH('22'))||'22' changing string '22' to '0022' ???
Knut Stolze - 19 Dec 2005 13:42 GMT > to help to Replace the stupid Coding like > SUBSTR('0000',LENGTH('22'),4-LENGTH('22'))||'22' > changing string '22' to '0022' ??? That is really stupid and slow coding. I would replace it if I had the chance.
 Signature Knut Stolze DB2 Information Integration Development IBM Germany
Tonkuma - 19 Dec 2005 13:56 GMT Some ideas: SUBSTR('000'||'22',LENGTH('22')) SUBSTR(RIGHT('0000'||'22',4),1,4) DIGITS(DECIMAL('22',4,0))
Knut Stolze - 19 Dec 2005 14:22 GMT > Some ideas: > SUBSTR('000'||'22',LENGTH('22')) > > SUBSTR(RIGHT('0000'||'22',4),1,4) > > DIGITS(DECIMAL('22',4,0)) I assumed that the given expression actually consisted of constants only. So there would be no need to do the calculation on the constants but rather write the results there directly.
 Signature Knut Stolze DB2 Information Integration Development IBM Germany
Brian Tkatch - 19 Dec 2005 16:45 GMT To make that itself shorter:
REPEAT('0', 4 - LENGTH('22')) || '22'
B.
Bob Stearns - 19 Dec 2005 22:19 GMT > To make that itself shorter: > > REPEAT('0', 4 - LENGTH('22')) || '22' > > B. Which is not quite the best you can do if the '22' in the example really stands for a CHARACTER expression. RIGHT('0000'||'22',4) is, I think, the best in the case of a CHARACTER expression.
Under the presumption that the '22' in the example really stands for an integer expression, a more general solution is RIGHT(CHAR(10000+22),4). The 10000 and the 4 in the expression can be adjusted trivially for any reasonable number of leading zeroes. This is optimal in that it requires only one evaluation of the expression. Negative numbers, decimal expressions, etc. are left as an exercise for the interested reader :-)
Tonkuma - 20 Dec 2005 03:00 GMT REPEAT and RIGHT are SYSFUN schema, and returns VARCHAR(4000). This fact sometime make things problematic or troublesome. For example, if you use CLP or Command Editor, a long column appears in the result. ------------------------- Commands Entered ------------------------- VALUES REPEAT('0', 4 - LENGTH('22')) || '22'; -------------------------------------------------------------------
1
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0022
1 record(s) selected.
So, I enclosed those expressions by SUBSTR. Like following.
------------------------ Commands Entered ------------------------- VALUES SUBSTR(RIGHT('0000'||'22',4),1,4); ------------------------------------------------------------------- VALUES SUBSTR(RIGHT('0000'||'22',4),1,4)
1 ---- 0022
1 record(s) selected.
Brian Tkatch - 20 Dec 2005 15:29 GMT Yeah, i know, the CLP is my main environment. :)
But inside a script, it shouldn't matter much, as the declaration of the variable should handle it. (Unless of course, he's using a version 7 Windows client to connect via ODBC to a version 8 Solaris server, in which case a SUBSTR would crash the server when the last parameter was out of range. :) )
B.
heming_g@hotmail.com - 20 Dec 2005 09:30 GMT ah.. i quite agree with you , and i write a function to do that
-----------------------------
CREATE FUNCTION F_FILLSTR(V_TAR VARCHAR(256),V_CNT INTEGER ) RETURNS VARCHAR(512) LANGUAGE SQL NO EXTERNAL ACTION BEGIN ATOMIC DECLARE COMSTR VARCHAR(256); SET COMSTR = SPACE(V_CNT); SET COMSTR = REPLACE(COMSTR,' ','0'); SET COMSTR = COMSTR||RTRIM(V_TAR); SET COMSTR = RIGHT(COMSTR,V_CNT); RETURN COMSTR; END@
-----------------------------
run "values f_fillstr('aa',8)" : result 000000aa
then i make a more general one and compile it successfully , but when i runs , a trouble comes , saying "SQL0440N No authorized routine name F_FILLSTR .... "
-----------------------------
CREATE FUNCTION F_FILLSTR(V_TAR VARCHAR(256),V_SUSTR CHAR(1),V_CNT INTEGER ) RETURNS VARCHAR(512) LANGUAGE SQL NO EXTERNAL ACTION BEGIN ATOMIC DECLARE COMSTR VARCHAR(256); SET COMSTR = SPACE(V_CNT); SET COMSTR = REPLACE(COMSTR,' ',V_SUSTR); SET COMSTR = COMSTR||RTRIM(V_TAR); SET COMSTR = RIGHT(COMSTR,V_CNT); RETURN COMSTR; END@
-----------------------------
any help again ?? thx a lot .
heming_g@hotmail.com - 20 Dec 2005 10:01 GMT ah.. i quite agree with you , and i write a function to do that .
-----------------------------
CREATE FUNCTION F_FILLSTR(V_TAR VARCHAR(256),V_CNT INTEGER ) RETURNS VARCHAR(512) LANGUAGE SQL NO EXTERNAL ACTION BEGIN ATOMIC DECLARE COMSTR VARCHAR(256); SET COMSTR = SPACE(V_CNT); SET COMSTR = REPLACE(COMSTR,' ','0'); SET COMSTR = COMSTR||RTRIM(V_TAR); SET COMSTR = RIGHT(COMSTR,V_CNT); RETURN COMSTR; END@
----------------------------- run "values f_fillstr('aa',8)" : result 000000aa
then i make a more general one and compile it successfully , but when i runs , a trouble comes , saying "SQL0440N No authorized routine name F_FILLSTR .... "
-----------------------------
CREATE FUNCTION F_FILLSTR(V_TAR VARCHAR(256),V_SUSTR CHAR(1),V_CNT INTEGER ) RETURNS VARCHAR(512) LANGUAGE SQL NO EXTERNAL ACTION BEGIN ATOMIC DECLARE COMSTR VARCHAR(256); SET COMSTR = SPACE(V_CNT); SET COMSTR = REPLACE(COMSTR,' ',V_SUSTR); SET COMSTR = COMSTR||RTRIM(V_TAR); SET COMSTR = RIGHT(COMSTR,V_CNT); RETURN COMSTR; END@
-----------------------------
any help again ?? thx a lot .
Tonkuma - 20 Dec 2005 13:22 GMT My guess is that you use the function like F_FILLSTR('aa', 'x', 8). If so, V_SUSTR CHAR(1) is the root of problem. Try V_SUSTR VARCHAR(1). Because data type of string constant(for example 'x') is VARCHAR.
Brian Tkatch - 20 Dec 2005 15:26 GMT >Which is not quite the best you can do if the '22' in the example really >stands for a CHARACTER expression. RIGHT('0000'||'22',4) is, I think, >the best in the case of a CHARACTER expression. Mostly, i was reponding how to keep the current logic there, so i used REPEAT instead of SUBSTR.
For myself, i agree i would use RIGHT('0000'||'22',4), because it makes more sense to me. But, exactly why do you think it best? Just because its straight forward? Or is there something i am missing?
B.
Bob Stearns - 20 Dec 2005 15:38 GMT >>Which is not quite the best you can do if the '22' in the example really >>stands for a CHARACTER expression. RIGHT('0000'||'22',4) is, I think, [quoted text clipped - 8 lines] > > B. Single evaluation of the parameter '22'. It makes no sense if it is a constant, no difference if it is a single column, but it could be very important if it were a complex expression in its own right. Good coding practice, even in the simple cases, will lead to better results in the harder ones.
Brian Tkatch - 20 Dec 2005 15:52 GMT
|
|
|