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.
8 14755
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
If you have any problems in writing the function, then I can help you out.
Regards
-- Sanjay
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
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
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
Hi,
for a start you should change the NULL values to '0'. After this, the following UPDATE stmt should do: - update
-
mytable
-
set
-
numtxtcol = (
-
concat(
-
substr( '000000', 1, 6-length( numtxtcol ) ),
-
numtxtcol
-
)
-
)
-
where
-
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
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: - ALTER TABLE
-
my.table
-
ADD COLUMN
-
padded_int
-
GENERATED ALWAYS AS
-
(
-
concat(
-
substr( '000000', 1, 5 - int(log10( int_col )) ),
-
char( int_col )
-
)
-
) ;
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
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: - ALTER TABLE
-
my.table
-
ADD COLUMN
-
padded_int
-
GENERATED ALWAYS AS
-
(
-
concat(
-
substr( '000000', 1, 5 - int(log10( int_col )) ),
-
char( int_col )
-
)
-
) ;
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......
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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 ...
|
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....
|
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
|
by: Ale |
last post by:
can we use Lpad() or Rpad() in insert statement?.............
if yes, kindly help me out with the query.....
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
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,...
|
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...
| |