I have two tables, Table_1 and Table_2. I want to compare one of the columns between the tables and have an output of the difference.
Specifically, I need to know which entries (of a column) of Table_1 are not present in table 2. I am using the EXCEPT operator but the numbers aren’t correct so I’d like to know how to make it case insensitive.
Here is my basic approach (without the case insensitive stuff):
SELECT "Address" FROM Table_1
EXCEPT
SELECT "People Address" FROM Table_2
So I thought I’d use:
SELECT "Address" FROM Table_1
EXCEPT
SELECT "People Address" FROM Table_2
To make it case insensitive, I’ve tried:
SELECT "Address" COLLATE Latin1_General_CI_AS FROM Table_1
EXCEPT
SELECT "People Address" COLLATE Latin1_General_CI_AS FROM Table_2
With the approoach above, I am getting the following error:
db query error: pq: collation "Latin1_General_CI_AS" for encoding "UTF8" does not exist.
How can I include case insensitivity? This is in PostgresSQL
3
Answers
COLLATE Latin1_General_CI_AS is MS SQL server syntax. So I ended up using the UPPER keyword like this:
Thanks to the comments posted in the question by @sergey
The error you specified in PostgreSQL is due to the fact that the Latin1_General_CI_AS statement specific to SQL Server is not recognized in PostgreSQL. To correct this error, you can use the LOWER or UPPER functions to make a case-insensitive comparison in PostgreSQL.
Below I show you how you can write a query to compare address columns and find case-insensitive differences:
You can create a case insensitive collation:
https://www.postgresql.org/docs/current/collation.html
And use it as expected:
Demo: https://dbfiddle.uk/lFOBATce