If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Fix SSN field leading zero

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-07-10, 07:17
nitingautam nitingautam is offline
Registered User
 
Join Date: Nov 2008
Location: Delhi, India
Posts: 15
Fix SSN field leading zero

Hi,

We have a Table in which SSN was specified as INTEGER hence it was removing the leading zeros of SSN ID.
For example 001234567 is getting saved as 1234567.

To resolve this issue, now we are converting the field type to varchar, this will fix issue for all new records but how to fix issue for existing records.

Means, I want to add the leading zeros as specified in below example to fix old values:

OLD SSN (in integer field)= 123456
NEW SSN (in varchar field)= 000123456

OLD SSN=1234567
NEW SSN=001234567



Thanks,
Reply With Quote
  #2 (permalink)  
Old 06-07-10, 07:30
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Code:
------------------------------ Commands Entered ------------------------------
SELECT old_ssn
     , DIGITS( old_ssn ) AS new_ssn
  FROM (VALUES INTEGER(123456) ,INTEGER(1234567) ) AS q(old_ssn);
------------------------------------------------------------------------------

OLD_SSN     NEW_SSN   
----------- ----------
     123456 0000123456
    1234567 0001234567

  2 record(s) selected.

Last edited by tonkuma; 06-07-10 at 07:34.
Reply With Quote
  #3 (permalink)  
Old 06-07-10, 09:59
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
SSN is 9 digits number.

So, we have to use:

Right(DIGITS(old_ssn), 9) AS new_ssn

Lenny
Reply With Quote
  #4 (permalink)  
Old 06-09-10, 03:24
nitingautam nitingautam is offline
Registered User
 
Join Date: Nov 2008
Location: Delhi, India
Posts: 15
Quote:
Originally Posted by tonkuma View Post
Code:
------------------------------ Commands Entered ------------------------------
SELECT old_ssn
     , DIGITS( old_ssn ) AS new_ssn
  FROM (VALUES INTEGER(123456) ,INTEGER(1234567) ) AS q(old_ssn);
------------------------------------------------------------------------------

OLD_SSN     NEW_SSN   
----------- ----------
     123456 0000123456
    1234567 0001234567

  2 record(s) selected.
tonkuma in the above example you used a virtual data. means not exist in table, what we say this concept???

If I want to create the same above thing but with two values means in pair , how to do it???
Reply With Quote
  #5 (permalink)  
Old 06-09-10, 05:10
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
tonkuma in the above example you used a virtual data. means not exist in table, what we say this concept???

If I want to create the same above thing but with two values means in pair , how to do it???
Although, I couldn't understand well second sentence,
I hope that following example may give you some ideas.

Tested on
Database server = DB2/NT 9.7.2

CREATE TABLE and INSERT data:
Code:
------------------------------ Commands Entered ------------------------------
CREATE TABLE nitingautam.t1
( ssn_id INTEGER
);
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
INSERT INTO nitingautam.t1
VALUES 123456;
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
SELECT * FROM nitingautam.t1;
------------------------------------------------------------------------------

SSN_ID     
-----------
     123456

  1 record(s) selected.
ALTER and REORG TABLE:
Code:
------------------------------ Commands Entered ------------------------------
ALTER TABLE nitingautam.t1
ALTER COLUMN ssn_id
  SET DATA TYPE VARCHAR(9);
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
REORG TABLE nitingautam.t1;
------------------------------------------------------------------------------
DB20000I  The REORG command completed successfully.
INSERT new data:
Code:
------------------------------ Commands Entered ------------------------------
INSERT INTO nitingautam.t1
VALUES '001234567';
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
SELECT * FROM nitingautam.t1;
------------------------------------------------------------------------------

SSN_ID   
---------
123456   
001234567

  2 record(s) selected.
UPDATE old data:
Code:
------------------------------ Commands Entered ------------------------------
UPDATE nitingautam.t1
   SET ssn_id = RIGHT('00000000' || ssn_id , 9)
 WHERE LENGTH( RTRIM(ssn_id) ) < 9
;
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
SELECT * FROM nitingautam.t1;
------------------------------------------------------------------------------

SSN_ID   
---------
000123456
001234567

  2 record(s) selected.

Last edited by tonkuma; 06-09-10 at 05:16. Reason: Add "Teste on Database server = DB2/NT 9.7.2"
Reply With Quote
Reply

Tags
ssn

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On