MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

RPAD / LPAD function in DB2

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
showkath
Beginner


Joined: 23 Mar 2005
Posts: 17
Topics: 7

PostPosted: Tue Jun 07, 2005 5:58 am    Post subject: RPAD / LPAD function in DB2 Reply with quote

Hi,

Could anyone please let me know whether
DB2 has RPAD / LPAD function similar to Oracle.
_________________
Regards,
Showkath
-----------------------------------------------------
Our opinions do not really blossom into fruition until we have expressed them to someone else - Mark Twain
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Tue Jun 07, 2005 8:11 am    Post subject: Reply with quote

Showkath,

As far as I know DB2 does not have direct function which relates to the LPAD/RPAD (left/right padding of a specific character) function of oracle.

However there are many ways to achieve the desired results. Simplest being using Concatenation of the desired string.

Please show us an example of the data and desired output along with the column definitions.

kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
schintala
Beginner


Joined: 18 May 2005
Posts: 108
Topics: 31
Location: USA

PostPosted: Tue Jun 07, 2005 2:10 pm    Post subject: Reply with quote

DB2 doesn't have the functions LPAD/RPAD which are on Oracle. Alternatively can't you make use of LTRIM/RTRIM?
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Tue Jun 07, 2005 3:03 pm    Post subject: Reply with quote

Quote:

Alternatively can't you make use of LTRIM/RTRIM?


Schintala,

LTRIM/RTRIM are the exact opposites of LPAD/RPAD function of oracle.

In Oracle, the LPAD function pads the left-side of a string with a specific set of characters where as the LTRIM function removes blanks from the beginning of a string expression.

In Oracle, the RPAD function pads the right-side of a string with a specific set of characters where as the RTRIM function removes blanks from the end of a string expression.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
showkath
Beginner


Joined: 23 Mar 2005
Posts: 17
Topics: 7

PostPosted: Tue Jun 07, 2005 10:29 pm    Post subject: Actual requirement... Reply with quote

Kolusu,

Thanks for your timely reply.

Let us consider the following example.

A table TMP001 has a column MAIL_ID of type char(10) which
would have values in the form COMP001, COMP002, COMP003.......

The requirement is to
i) select the maximum value or the latest MAIL_ID
and show the next sequence. If maximum value is COMP003
then the select should return COMP004 and;
ii) if no row exists
it should return COMP0001.

For the above, I have written the following query

SELECT DISTINCT COALESCE(
SUBSTR(MAIL_ID,1,4) || SUBSTR(STRIP(CHAR(XX.VAL1)),3,4),
'COMP0001')
FROM TMP001, TABLE
(SELECT MAX(CAST(SUBSTR(MAIL_ID,5) AS DEC)+1) AS VAL1 FROM TMP001) XX

This query works fine for the requirement no i). But for Req ii) query does
not returns anything.

Another problem is, while converting to DEC and concatenation
extra zeroes are padded towards the left part. For this again
I have to use substr.. 3,4 to extract the required part. So I
thought that If I use INT() function while generating the next seq
, it would return only integer part. If LPAD fn. is used for req.
No. of zeroes, then process would be simple.

Sorry for lengthy reply.
_________________
Regards,
Showkath
-----------------------------------------------------
Our opinions do not really blossom into fruition until we have expressed them to someone else - Mark Twain
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Wed Jun 08, 2005 5:34 am    Post subject: Reply with quote

Quote:

Another problem is, while converting to DEC and concatenation
extra zeroes are padded towards the left part. For this again
I have to use substr.. 3,4 to extract the required part.


Showkath,

Why are picking only 4 bytes? what happens once the counter reaches 10,000? Would start over once again?

Ideally I would think of COMP followed 6 digit numeric field which would go up to atleast 999,999.

ie. COMP000001

Fetching a value of 1 is very easy as you can add UNION clause and another select to get 1 as the seqnum and order it by the seqnum desc so that you have the max as the first row and use FETCH FIRST 1 row only which gives you only 1 row at a time.

EX:
Code:

SELECT COALESCE(CHAR('COMP') CONCAT                                   
       A.MAX_VAL,CHAR('COMP000001'))                               
  FROM (                                                           
SELECT DIGITS(MAX(DEC(REPLACE(MAIL_ID,'COMP','0'),5)) + 1) MAX_VAL
  FROM TABLE                                                         
UNION                                                             
SELECT DIGITS(DEC('1',6)) MAX_VAL                                 
  FROM SYSIBM.SYSDUMMY1) A                                         
ORDER BY 1 DESC                                                   
FETCH FIRST 1 ROW ONLY                                             
;


Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
showkath
Beginner


Joined: 23 Mar 2005
Posts: 17
Topics: 7

PostPosted: Wed Jun 08, 2005 6:07 am    Post subject: Reply with quote

Kolusu,

Thanks for your reply.
_________________
Regards,
Showkath
-----------------------------------------------------
Our opinions do not really blossom into fruition until we have expressed them to someone else - Mark Twain
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group