IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

LPAD Function


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
sun_job

New User


Joined: 18 Sep 2007
Posts: 73
Location: Bangalore

PostPosted: Wed Nov 28, 2012 12:35 pm
Reply with quote

My requirement is to have two characters in column.
Here is the scenario :-
My table column is a int(2) which contains values ranging from 0(null) to 2 I want to have for values whose length is less than 2 should be padded with zero

example:-
null should be updated to 00
1 should be updated to 01
12 should be updated to 12



Help would be really appreciated.

Tried with LPAD function , but not working...
An expert help is appreciated
Back to top
View user's profile Send private message
Pandora-Box

Global Moderator


Joined: 07 Sep 2006
Posts: 1592
Location: Andromeda Galaxy

PostPosted: Wed Nov 28, 2012 12:47 pm
Reply with quote

Quote:
Tried with LPAD function , but not working...


Share us the query you had used.
Back to top
View user's profile Send private message
sun_job

New User


Joined: 18 Sep 2007
Posts: 73
Location: Bangalore

PostPosted: Wed Nov 28, 2012 12:51 pm
Reply with quote

Code:
SELECT
           CHAR(DEPT_I) AS DEPTID,
           LPAP(CHAR(CLA_I),2,'0') AS CLASSID
         FROM DB2PD.CLASS;
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Wed Nov 28, 2012 1:06 pm
Reply with quote

And would it be possible for you to reveal to us the manner in which it "does not work"?
Back to top
View user's profile Send private message
Pandora-Box

Global Moderator


Joined: 07 Sep 2006
Posts: 1592
Location: Andromeda Galaxy

PostPosted: Wed Nov 28, 2012 1:42 pm
Reply with quote

Sorry to not say it first time

Please share the output yo get and also the ouput you expect for the set of data in table
Back to top
View user's profile Send private message
sun_job

New User


Joined: 18 Sep 2007
Posts: 73
Location: Bangalore

PostPosted: Wed Nov 28, 2012 1:51 pm
Reply with quote

It seems LPAD function does not work. I have tried to do it with CASE expression.
Code:
(CASE
     WHEN LENGTH(LTRIM(RTRIM(CHAR(C.CLA_I))))=1 then '0'||LTRIM(RTRIM(CHAR(C.CLA_I)))    
     WHEN LENGTH(LTRIM(RTRIM(CHAR(C.CLA_I))))=2 then      LTRIM(RTRIM(CHAR(C.CLA_I)))    
   END) AS CLASSID,


This is working as expected
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Wed Nov 28, 2012 2:03 pm
Reply with quote

Hi,

Check for the function DIGITS in DB2 Manual

You may need to handle Null values using CASE statement

Regards,
Chandan
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Wed Nov 28, 2012 2:05 pm
Reply with quote

afaik there is no such thing as int(2) in DB2 but you could just use :
Code:
right(digits(i2),2)

or if i2 is nullable :
Code:
right(digits(coalesce(i2,0)),2)
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Wed Nov 28, 2012 2:12 pm
Reply with quote

Thanks Guyc

I have totally missed coalesce while suggesting CASE statement in my earlier post

Regards,
Chandan
Back to top
View user's profile Send private message
sun_job

New User


Joined: 18 Sep 2007
Posts: 73
Location: Bangalore

PostPosted: Thu Nov 29, 2012 11:17 am
Reply with quote

chandan.inst wrote:
Hi,

Check for the function DIGITS in DB2 Manual

You may need to handle Null values using CASE statement

Regards,
Chandan


Sure Chandan , will check the same
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Calling an Open C library function in... CICS 1
No new posts DATE2 function SYNCSORT 15
No new posts Help on PL/I jsonPutValue function PL/I & Assembler 8
No new posts how to use Tso outtrap external function All Other Mainframe Topics 8
No new posts INSYNC option with same function as I... JCL & VSAM 0
Search our Forums:

Back to Top