Search code examples
sql-serversqldatatypes

Error in storing values in SQL database table


In my table, there is a column called zipcode whose datatype is int. And when I am storing a zipcode which starts with 0 (for eg. 08872), it is getting stored as 8872.

Can anybody explain me why is it happening?


Solution

  • An INT value is numeric - and numerically, 08872 and 8872 are identical - both represent the value 8872.

    SQL Server will not store leading zeroes for numerical values. That's just the way it is.

    Either store this as CHAR(5) instead, or handle the formatting (adding leading zeroes to your zip codes) on the frontend when you need to display it.