Hello I have a postgresql question about searching on zip codes. Basicly, if I do a:
select * from my_table where zip_ = '90210';
I am returned 0 rows. I know for a fact that there is thousands of rows with that zip code. The table itself is over a million rows.
Column | Type | Modifiers
---------------------+-------------------+-----------------------------------------------------
...
zip_ | character varying |
Here is my table and I am not able to search effectively on zip_ column. The table does return a search if I use the like keyword along with % char, but that is not what I need.
my questions are:
- why when i search on zip_ lets say '90210' it comes back with 0 rows when I know there is thousands of records with the zip code of '90210'?
- if i change the type to int would that make a difference?
- do i need an index on the zip_ column?
where zip_ LIKE '%90210%'return rows? – ypercubeᵀᴹ Apr 28 '18 at 19:30select * from my_table where trim(zip_) = '90210';. Does that give you any rows? (And don't change the type tointat least if you want international addresses. Have a look at the British post code system. They have letters in them as well.) – sticky bit Apr 28 '18 at 19:34select zip_, decode(zip_ 'escape') from my_table where zip_ LIKE '%90210%' limit 5 ;and show us the output. As sticky_bit said, you probably have some whitespace/invisible characters there. – ypercubeᵀᴹ Apr 28 '18 at 19:38textis a little overkill? I mean yes, there might be some funny postal systems in this world but a ZIP with unlimited length? I doubt that is needed. Choose some generous length like 255 and you should be good. Don't forget people are supposed to write those on letter envelopes. That implies a certain limit anyway. Besides such BLOB/CLOB columns can have an impact on performance. So if not really needed, it's not a bad idea to avoid them. – sticky bit Apr 28 '18 at 20:25