Skip to main content
Question Protected by Ken White
edited tags
Link
animuson
  • 54.9k
  • 28
  • 142
  • 150
edited tags
Link
Kevin Reid
  • 46k
  • 14
  • 99
  • 126
Cleaned everything up nicely for first time Google-search visitors
Source Link
Sean Hanley
  • 5.8k
  • 7
  • 45
  • 55

From first glance, it would appear I have two basic choices for storing ZIP codes in a database table:

  1. Text (probably most common), i.e. char(5) or possibly varchar(9) to support +4 extension
  2. Numeric (technically more efficient size-wise), i.e. 32-bit integer

Both would satisfy the requirements of the data, if we assume that there are no international concerns. In the past we've generally just gone the text route, but I was wondering if anyone does the opposite? Just from brief comparison it looks like the integer method has two clear advantages. It is, by means of its nature, automatically limited to numerics only (whereas without validation the text style could store letters and such which are not, to my knowledge, ever valid in a ZIP code). Additionally, it takes less space, being 4 bytes (which should be plenty even for 9-digit ZIP codes) instead of 5 or 9 bytes.:

  • It is, by means of its nature, automatically limited to numerics only (whereas without validation the text style could store letters and such which are not, to my knowledge, ever valid in a ZIP code). This doesn't mean we could/would/should forgo validating user input as normal, though!
  • It takes less space, being 4 bytes (which should be plenty even for 9-digit ZIP codes) instead of 5 or 9 bytes.

Also, it seems like it wouldn't hurt display output much, as it. It is trivial to slap a ToString() on a numeric value, even using simplyuse simple string manipulation to insert a hyphen or space or whatever for the +4 extension, so I don't think there'd be any hurdles when actually using it in a web application settingand use string formatting to restore leading zeroes.

Is there anything I'm missing that would discourage using intint as a datatype for ZIP codes?

UPDATE: Yes, this will only ever be used for US-based postal codes. 95% of them will probably all beonly ZIP codes from the same state, even. As for leading zeroes, couldn't you just do a ToString with a format string to add leading zeroes? I.e.: .ToString("00000")

From first glance, it would appear I have two basic choices for storing ZIP codes in a database table:

  1. Text (probably most common), i.e. char(5) or possibly varchar(9) to support +4 extension
  2. Numeric (technically more efficient size-wise), i.e. 32-bit integer

Both would satisfy the requirements of the data. In the past we've generally just gone the text route, but I was wondering if anyone does the opposite? Just from brief comparison it looks like the integer method has two clear advantages. It is, by means of its nature, automatically limited to numerics only (whereas without validation the text style could store letters and such which are not, to my knowledge, ever valid in a ZIP code). Additionally, it takes less space, being 4 bytes (which should be plenty even for 9-digit ZIP codes) instead of 5 or 9 bytes.

Also, it seems like it wouldn't hurt display output much, as it is trivial to slap a ToString() on a numeric value, even using simply string manipulation to insert a hyphen or space or whatever for the +4 extension, so I don't think there'd be any hurdles when actually using it in a web application setting.

Is there anything I'm missing that would discourage using int as a datatype for ZIP codes?

UPDATE: Yes, this will only ever be used for US-based postal codes. 95% of them will probably all be ZIP codes from the same state, even. As for leading zeroes, couldn't you just do a ToString with a format string to add leading zeroes? I.e.: .ToString("00000")

From first glance, it would appear I have two basic choices for storing ZIP codes in a database table:

  1. Text (probably most common), i.e. char(5) or varchar(9) to support +4 extension
  2. Numeric, i.e. 32-bit integer

Both would satisfy the requirements of the data, if we assume that there are no international concerns. In the past we've generally just gone the text route, but I was wondering if anyone does the opposite? Just from brief comparison it looks like the integer method has two clear advantages:

  • It is, by means of its nature, automatically limited to numerics only (whereas without validation the text style could store letters and such which are not, to my knowledge, ever valid in a ZIP code). This doesn't mean we could/would/should forgo validating user input as normal, though!
  • It takes less space, being 4 bytes (which should be plenty even for 9-digit ZIP codes) instead of 5 or 9 bytes.

Also, it seems like it wouldn't hurt display output much. It is trivial to slap a ToString() on a numeric value, use simple string manipulation to insert a hyphen or space or whatever for the +4 extension, and use string formatting to restore leading zeroes.

Is there anything that would discourage using int as a datatype for US-only ZIP codes?

edited tags
Link
Shog9
  • 160.1k
  • 36
  • 237
  • 242
Loading
don't use broad titles for specific questions
Source Link
Shog9
  • 160.1k
  • 36
  • 237
  • 242
Loading
Added some more details in response to early answers
Source Link
Sean Hanley
  • 5.8k
  • 7
  • 45
  • 55
Loading
Source Link
Sean Hanley
  • 5.8k
  • 7
  • 45
  • 55
Loading