PostgreSQL notes

Some things I’ve learnt recently.

Simple boolean function to check if a column exists

SELECT EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name = tabname AND column_name = col1)

Identify duplicate values in columns

SELECT key FROM tabname GROUP BY key HAVING (COUNT(key) > 1)

col1 is an integer but I wanted it formatted as text in my python script

SELECT CAST(col1 AS TEXT) FROM tabname

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s