469,282 Members | 1,704 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Similar LPAD function in DB2

Hi All,
I am new to DB2 and was just wondering how to update my existing table a column of which has to be left padded with zeros.
Here is the scenerio :-
My table column is a varchar(30) which contains characters ranging from 0(null) to 30 now what i want to do is for those characters whose length is less than 6 should be padded with zero and leave the rest as it is ..also i would like the values to be the same with just the zeros padded wherever required.

example:-
null should be updated to 000000
1 should be updated to 000001
12 should be updated to 000012
123 should be updated to 000123
.....
123456 should be updated to 123456


Help would be really appreciated.
Mar 10 '08 #1
8 14268
sakumar9
127 Expert 100+
Hi All,
I am new to DB2 and was just wondering how to update my existing table a column of which has to be left padded with zeros.
Here is the scenerio :-
My table column is a varchar(30) which contains characters ranging from 0(null) to 30 now what i want to do is for those characters whose length is less than 6 should be padded with zero and leave the rest as it is ..also i would like the values to be the same with just the zeros padded wherever required.

example:-
null should be updated to 000000
1 should be updated to 000001
12 should be updated to 000012
123 should be updated to 000123
.....
123456 should be updated to 123456


Help would be really appreciated.
There is no such direct function. Alternatively you can have some function that will do the required concatenation. For this, you can use CONCAT() function to manually do the concatenation depending upon the string size.


Regards
-- Sanjay
Mar 10 '08 #2
sakumar9
127 Expert 100+
If you have any problems in writing the function, then I can help you out.

Regards
-- Sanjay
Mar 10 '08 #3
If you have any problems in writing the function, then I can help you out.

Regards
-- Sanjay

I think that would be really helpful if you can show me in detail how to achieve this.....

Thanks alot
Mar 10 '08 #4
sakumar9
127 Expert 100+
Sure, I will try to give you the code, but that may not be possible today. I can send you the code by tomorrow.

Regards
-- Sanjay
Mar 10 '08 #5
Sure, I will try to give you the code, but that may not be possible today. I can send you the code by tomorrow.

Regards
-- Sanjay
That is quite fine by me.... I will be looking forward to this.....

Thanks
Mar 10 '08 #6
docdiesel
297 Expert 100+
Hi,

for a start you should change the NULL values to '0'. After this, the following UPDATE stmt should do:

Expand|Select|Wrap|Line Numbers
  1. update
  2.   mytable
  3. set
  4.   numtxtcol = (
  5.     concat(
  6.       substr( '000000', 1, 6-length( numtxtcol ) ),
  7.       numtxtcol
  8.     )
  9.   )
  10. where
  11.   length( numtxtcol ) < 6
Means, slice the leading zero chars you need from the fixed string '000000' and concatenate them with your varchar value before replacing the original value with the concatenated one - if it's less than six chars.

Regards,

Bernd
Mar 10 '08 #7
docdiesel
297 Expert 100+
Hi,

hm, I missed your discussion. These things happen if you leave your browser window open for hours ... do you need to do this just once, repeatedly or every time data is written to the varchar() column?

If you need to do this just once, my script from above should be fine. If you need to have it left padded with zeros every time something gets written to that column, I suggest some other path: Modify that column to a nullable integer column and add a second which is generated automatically with sql like the following:

Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE
  2.   my.table
  3. ADD COLUMN
  4.   padded_int
  5. GENERATED ALWAYS AS
  6. (
  7.   concat(
  8.     substr( '000000', 1,  5 - int(log10( int_col ))  ),
  9.     char( int_col )
  10.   )
  11. )  ;
This is just to show how to do it so I've not included the code for the NULL values. But you may easily solve this by adding a CASE statement (NULL -> '000000').

Regards,

Bernd
Mar 10 '08 #8
Hi,

hm, I missed your discussion. These things happen if you leave your browser window open for hours ... do you need to do this just once, repeatedly or every time data is written to the varchar() column?

If you need to do this just once, my script from above should be fine. If you need to have it left padded with zeros every time something gets written to that column, I suggest some other path: Modify that column to a nullable integer column and add a second which is generated automatically with sql like the following:

Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE
  2.   my.table
  3. ADD COLUMN
  4.   padded_int
  5. GENERATED ALWAYS AS
  6. (
  7.   concat(
  8.     substr( '000000', 1,  5 - int(log10( int_col ))  ),
  9.     char( int_col )
  10.   )
  11. )  ;
This is just to show how to do it so I've not included the code for the NULL values. But you may easily solve this by adding a CASE statement (NULL -> '000000').

Regards,

Bernd

okey... thank you for the help...it is working for me....as you suggested i changed the null values first then applied the first update statement.....
Just for your info i have to use it just for one time as the expected data from then on would be in the proper format...

Thanks again for your valuable input......
Mar 11 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by anoj | last post: by
1 post views Thread by Peter Parker | last post: by
3 posts views Thread by Ale | last post: by
39 posts views Thread by vlsidesign | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.