In PostgreSQL, the COALESCE()
operator returns the first non-null value from its list of arguments. If all arguments are null
, it returns null
.
Category: PostgreSQL
2 Ways to Delete Duplicate Rows in PostgreSQL (Ignoring the Primary Key)
Below are two options for removing duplicate rows from a table in PostgreSQL when those rows have a primary key or unique identifier column. The primary key is used in the query, but it’s ignored when comparing duplicates (due to the fact that primary keys prevent duplicate rows by definition).
The following examples delete duplicate rows but keep one. So in the case of say, three identical rows, it deletes two and keeps one.
Continue readingSubtract Hours from a Time Value in PostgreSQL
In PostgreSQL, we can use the -
operator to subtract one or more hours from a time value.
By “time” value, this could be an actual time
value, a timestamp
, or an interval
. We can also subtract hours from a date
value or a date
and time
combination.
4 Ways to Find Rows that Contain Lowercase Letters in PostgreSQL
Here are four options for returning rows that contain lowercase characters in PostgreSQL.
Continue readingReturn the Unix Timestamp in SQL
Below are examples of using some of the more popular RDBMSs to return the Unix timestamp.
The Unix timestamp (also known as Unix Epoch time, Unix time, or POSIX time) is the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC).
Continue readingSQL IFNULL() Explained
Some RDBMSs provide an IFNULL()
function that can be used when dealing with potentially null values. In particular, MySQL, MariaDB, and SQLite each have an IFNULL()
function that replaces any null values with another value.
Other RDBMSs, such as SQL Server, Oracle, and PostgreSQL provide similar functionality via functions of a different name.
Continue readingFind All Non-Numeric Values in a Column in SQL
If you ever encounter a character column that should be numeric, there’s always a possibility that it contains non-numeric data that you don’t know about.
For example, someone might have set up a Price
column as a varchar
column that should have been a numeric
column, and now you need to clean up after them. You might start by identifying all non-numeric data so that you can work out what to do with it before converting the column to a numeric type.
In SQL, you can run a query to return non-numeric data from the column. The query you use will largely depend on your DBMS.
Continue readingFind Values That Don’t Contain Numbers in SQL
If you have a column in a database table that contains character data, but some rows also contain numbers, you can use the following SQL queries to return just those rows that don’t contain numbers within the value.
Continue readingSQL CASE Statement
In SQL, the CASE
statement evaluates a list of conditions and returns one of multiple possible result expressions.
In some ways, the SQL CASE
statement is kind of similar to the IF...ELSE
statement in that it allows us to check for a given condition and return a different result depending on the outcome.
GREATEST() Function in PostgreSQL
In PostgreSQL, the GREATEST()
function returns the greatest (or largest) value from a list of any number of expressions.
The GREATEST()
function is not included in the SQL standard, but it’s a popular extension that many major RDBMSs support.