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 > PostgreSQL > Comparing Columns In Posgre SQL

LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Registered User
Join Date: Jan 2013
Posts: 1
Comparing Columns In Posgre SQL


I am trying to weed out bad data in a Address table. I have an instance where
Column Address3 imported City, state, and zip and Column Zip also imported the zip code in random cases. I need to get rid of the zip code in these random cases. I want to run a comparison of Address3 and Zip to see where Address3 is LIKE Zip or vise versa so I can isolate the data. For example:

Column Address 3
Watertown, NY 15674

Column ZIP

What would be a query that would compare these? Thanks! X)
Reply With Quote
  #2 (permalink)  
Registered User
Join Date: Dec 2012
Posts: 32
Guessing ...:
Do you want to compare the five last characters from column "Address3" with column "ZIP" and you want all lines where these a different?
Assuming that column "ZIP" is a text or varchar column.
In the examlpe below you have to replace "Your_Table_Name" with the right table name of course.

SELECT Address3,ZIP 
 FROM Your_Table_Name
 WHERE substr(Address3,length(Address3)-4,5) <> ZIP;
See also the related functions in the official PostgreSQL documentation:
PostgreSQL: Documentation: 9.1: String Functions and Operators
Reply With Quote

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