Here’s an example of using SQL to find duplicate rows in a database table. This technique can be used in most of the major RDBMSs, including SQL Server, Oracle, MySQL, MariaDB, PostgreSQL, and SQLite.
Continue readingCategory: MySQL
2 Ways to Get the Definition of a Generated Column in MySQL
A generated column is one whose value is derived from an expression, as opposed to a fixed value. The expression typically uses other columns in the same table to compute the derived value.
We can create a generated column in MySQL very easily. But what if we want to go back later and see its definition?
Here are two options for returning the definition of a generated column in MySQL.
Continue readingHow to Convert a Unix Timestamp to a Date/Time in SQL
Here are examples of converting a Unix timestamp to a date/time value in some of the major RDBMSs.
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 readingCreate a Generated Column in MySQL
This article contains an example of adding a generated column to a table in MySQL.
Also known as computed columns, generated columns usually contain values that are dependent on other factors (such as the values in other columns).
Creating (or adding) a generated column in MySQL is basically the same as creating a normal column, except that the definition of the generated column contains an expression that determines the column’s value.
Continue readingReturn Rows that Contain Only Alphanumeric Characters in SQL
Alphanumeric characters are alphabetic characters and numeric characters.
Below are examples of using SQL to return rows that contain only alphanumeric characters.
Continue reading3 Ways to Get the First Day of the Month in MySQL
Here are three methods we can use to return the first day of a given month in MySQL.
This could be the first day of the current month, or the first day of the month based on a date that we specify.
Getting the first day of the month allows us to perform further calculations on the resulting date, like adding a certain number of days to the start of the month, etc.
Continue readingSQL ISNULL() Explained
Some RDBMSs provide an ISNULL()
function that can be used when dealing with potentially null values.
MySQL, MariaDB, and Oracle Database each have an ISNULL()
function that returns 1
if its argument is null
, and 0
if it’s not.
SQL Server also has an ISNULL()
function, but it works differently. It works more like how the IFNULL()
function works in some other RDBMSs.
Other RDBMSs, such as PostgreSQL and SQLite don’t include an ISNULL()
function, but they do support the IS NULL
predicate (as do the other RDBMSs).
MySQL CASE Statement
In MySQL, the CASE
statement can be used in stored programs to perform a complex conditional construct. It compares a list of conditions and returns a different result depending on which condition (if any) is matched.
The CASE
statement is different to the CASE
operator, in that the CASE
statement is specifically for use in stored programs. Also, there’s a slight difference in the syntax.
MySQL CASE Operator
In MySQL, the CASE
operator compares a list of conditions and returns one of multiple possible result expressions.
The CASE
expression is included in the SQL standard (ISO/IEC 9075), and most major RDBMSs support it.
MySQL also has the CASE
statement, which is slightly different to the CASE
operator. This article is about the CASE
operator.
Return 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 reading