View previous topic :: View next topic
Author
Message
Prasanthhere Active User Joined: 03 Aug 2005Posts: 306
Hi
I am having a table suppose its having 3 fields which are integer .Let the length of each field FIELD1 is 8,FIELD1 is 9,FIELD3 is 7 .I am having values 3333 4444 5555 in the corresponding fields .i want to write an SQL query which will pad zeores to the left how can we do that
Eg
Input table A
FIELD1 FIELD2 FIELD3
3333 4444 5555
I want the o/p to be
FIELD1 FIELD2 FIELD3
00003333 000004444 000555
Thanks in advance
Back to top
Hanfur Active User Joined: 21 Jun 2006Posts: 104
Prsanth,
If you want to pad with zero to coumn values use 0 in single quotes as given below..
Here CMPY,LEDGR,NAME are column's of table TABLE1.
SELECT ('0000'||CMPY),('00000'||LEDGR),('000'||NAME)
FROM DB2TST.TABLE1
CMPY --> padded with 0000 zeros
LEDGR --> padded with 00000 zeros and
Name --->padded with 000 zeros.
-Han.
Back to top
DavidatK Active Member Joined: 22 Nov 2005Posts: 700 Location: Troy, Michigan USA
Hello Han,
See if this is what you are looking for. If not, please come back with furthur expination.
Code:
DECLARE GLOBAL TEMPORARY TABLE TEMP_TBL
(
FIELD1 DEC(8),
FIELD2 DEC(9),
FIELD3 DEC(7)
)
;
INSERT INTO SESSION.TEMP_TBL (FIELD1, FIELD2, FIELD3)
VALUES (3333, 4444, 5555)
;
INSERT INTO SESSION.TEMP_TBL (FIELD1, FIELD2, FIELD3)
VALUES (26,72377, 0)
;
SELECT DIGITS(FIELD1) AS FIELD1,
DIGITS(FIELD2) AS FIELD2,
DIGITS(FIELD3) AS FIELD3
FROM SESSION.TEMP_TBL
;
Results of Query
Code:
+--------------------------------+
| FIELD1 | FIELD2 | FIELD3 |
+--------------------------------+
1_| 00003333 | 000004444 | 0005555 |
2_| 00000026 | 000072377 | 0000000 |
+--------------------------------+
Dave
Back to top
ravi17s Warnings : 1 New User Joined: 15 Aug 2003Posts: 57
Prasanth,
Putting togather David's and Hanfur's Ideas......This is what i get...
SELECT DIGITS(CAST(COLUMNN1 AS DECIMAL(8,2))),
DIGITS(CAST(COLUMNN2 AS DECIMAL(9,2))),
DIGITS(CAST(COLUMNN1 AS DECIMAL(7,2))),
FROM <tablename>
WHERE <CONDITIOn> ;
Please let me know if you need further clarification...
Thanks,
Ravi..........
Back to top
ravi17s Warnings : 1 New User Joined: 15 Aug 2003Posts: 57
This is more appropiate........than my previous query...
SELECT DIGITS(CAST(COLUMNN1 AS DECIMAL(8,0)) ),
DIGITS(CAST(COLUMNN2 AS DECIMAL(9,0))),
DIGITS(CAST(COLUMNN1 AS DECIMAL(7,0))),
FROM <tablename>
WHERE <CONDITIOn> ;
Thanks,
Ravi.........
Back to top
Please enable JavaScript!