473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Any function in DB2 to help

to help to Replace the stupid Coding like
SUBSTR('0000',LENGTH('22'),4-LENGTH('22'))||'22'
changing string '22' to '0022' ???

Dec 19 '05 #1
13 1620
he******@hotmail.com wrote:
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.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 19 '05 #2
Some ideas:
SUBSTR('000'||'22',LENGTH('22'))

SUBSTR(RIGHT('0000'||'22',4),1,4)

DIGITS(DECIMAL('22',4,0))

Dec 19 '05 #3
Tonkuma wrote:
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.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 19 '05 #4
To make that itself shorter:

REPEAT('0', 4 - LENGTH('22')) || '22'

B.

Dec 19 '05 #5
Brian Tkatch wrote:
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 :-)
Dec 19 '05 #6
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.

Dec 20 '05 #7
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 .

Dec 20 '05 #8
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 .

Dec 20 '05 #9
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.

Dec 20 '05 #10
>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.

Dec 20 '05 #11
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.

Dec 20 '05 #12
Brian Tkatch wrote:
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.

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.
Dec 20 '05 #13
Good point. Thanx.

B.

Dec 20 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Edward King | last post by:
Hi! I am trying to achieve the following: I have a number of help pages (in the format help_nn.php where nn=helpid). I want to be able to open a particular help page by calling the function...
4
by: Joneseyboy | last post by:
Hi, I'm new to this so I could really do with some help! I am using VB.net to create a small game. basically there is a Textbox, a button and a listbox. The user enters a number into the...
2
by: Chuck Martin | last post by:
I am having a most frustrating problem that references, web searches, and other resources are no help so far in solving. Basically, I'm trying to design a pop-up window to be called with a funciton...
1
by: intl04 | last post by:
I'm trying to set up a query that will include a new field ('Days until completion') whose value is derived from the DateDiff function. I think I have the syntax correct but am not sure. Days...
7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
4
by: George Durzi | last post by:
I created a simple user control which contains a hyperlink to link the user to a topic in a compiled help file. I named all my help topics to have the same name as the aspx they are for. So in...
7
by: Jimakos Bilakis | last post by:
Hi guys! I'm using the C++ Builder 6 Enterprise Edition where I create some tables in Paradox and with the help of a structure i pass my data from the form (Edit boxes) to the Paradox table with...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
1
by: Beamor | last post by:
function art_menu_xml_parcer($content, $showSubMenus) { $doc = new DOMDocument(); $doc->loadXML($content);//this is the line in question $parent = $doc->documentElement; $elements =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.