Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
I have a table named as Country as follows

SQL
admin1code
    1
    10
    11
    12
    5
    6
    45


I wants to update column data such that 0 should be appended at the beginning of number which are single unit EX. 1,2,3...9 etc becomes 01,02,03..09

so final column data after update should look like

SQL
admin1code
    01
    10
    11
    12
    05
    06
    45
Posted
Comments
Herman<T>.Instance 14-Mar-12 11:46am    
what is the datatype for admin1code? (n)varchar ?

assuming admin1code is of datatype (n)varchar
SQL
Update Country
set admin1code = right('00'+ admin1code, 2)
 
Share this answer
 
Comments
vikram_shinde 14-Mar-12 11:57am    
many thanks...
OriginalGriff 14-Mar-12 11:58am    
You might want a WHERE on that: what happens with 123, 124, etc.? :laugh:
Herman<T>.Instance 15-Mar-12 4:27am    
:smile:
but I guess het want 01 then to be 001
Try:
SQL
UPDATE Country SET admin1code = '0' + admin1code WHERE LEN(admin1code) = 1 AND admin1code >= '0' and admin1code <='9'
 
Share this answer
 
Comments
gvprabu 28-Sep-12 5:59am    
Nice :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900