473,386 Members | 1,791 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,386 software developers and data experts.

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 14755
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

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

Similar topics

7
by: Paul Sheer | last post by:
I need to automatically search and replace all fixed size buffer strcpy's with strncpy's (or better yet, strlcpy's) as a security and stability audit. The code base is large and it is not feasable...
2
by: Antitax | last post by:
I have a database with more than 800 adress records Some of the are similar because some letters in the street adress for example are not identical, altough they point to the same adress. Does...
1
by: anoj | last post by:
Hi All i need to write a function in vb.net similar to pack() in PHP kindly help. Thanks Anoj Kumar
5
by: Niyazi | last post by:
Hi, Does anyone knows any good code for string manipulation similar to RegularExpresion? I might get a value as string in a different format. Example: 20/02/2006 or 20,02,2006 or ...
2
by: techiesekar07 | last post by:
this is an oracle query SUBSTR(LPAD(xxxxx, 8, 0),3,6) Please provide me the db2 query for this....
1
by: Peter Parker | last post by:
I understand for php5 there is function filter_var as in filter_var($var, FILTER_SANITIZE_URL);Is there similar function in php4.3? Thank you
3
by: Ale | last post by:
can we use Lpad() or Rpad() in insert statement?............. if yes, kindly help me out with the query.....
39
by: vlsidesign | last post by:
Operators seem similar to functions. They both do something to either arguments or operands, but are different in their syntax. Operators seem to be like built-in C functions. It would seem that...
1
by: shenanwei | last post by:
I have 2 strings here which has 1 or 2 digits different because of typo, like below 179705723 179705732 Any simple way I can make them similar. soundex function is no use at all, it return...
1
by: Alexio | last post by:
I have two similar function that need to be combined. Individually they both work however combined one over rides the other. The code is attached below and an attachment is included for data...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.