SUBDATE() Examples – MySQL

In MySQL, you can use the SUBDATE() function to subtract a specified amount of time from a date. For example, you could use it to subtract 10 days from a given date. You can specify whether to subtract days, weeks, months, quarters, years, etc. You can also subtract a time value, such as seconds, microseconds, etc.

When using the first syntax listed below, the SUBDATE() function is a synonym for the DATE_SUB() function (similar to how ADDDATE() is a synonym for DATE_ADD() when using the same syntax).

Continue reading

DATE_SUB() Examples – MySQL

In MySQL, you can use the DATE_SUB() function to subtract a specified amount of time from a date. For example, you can use it to subtract 7 days from a given date. You can specify whether to subtract days, weeks, months, quarters, years, etc. You can also subtract a time value, such as seconds, microseconds, etc.

This function is similar to DATE_ADD(), except that it subtracts from a date instead of adds to it.

Continue reading

DATE_ADD() Examples – MySQL

In MySQL, you can use the DATE_ADD() function to add a specified amount of time to a date. For example, you could use it to add 5 days to a given date. You can specify whether to add days, weeks, months, quarters, years, etc. You can also add a time value, such as seconds, microseconds, etc.

This article contains examples that demonstrate how the DATE_ADD() function works.

Continue reading

ADDDATE() Examples – MySQL

In MySQL, you can use the ADDDATE() function to add a specified amount of time to a date. For example, you could use it to add 10 days to a given date. You can specify whether to add days, weeks, months, quarters, years, etc. You can also add a time value, such as seconds, microseconds, etc.

The ADDDATE() function is a synonym for the DATE_ADD() function (they both do the same thing) when using the first syntax listed below.

This article contains examples to demonstrate usage of the ADDDATE() function.

Continue reading

4 Ways to Replace NULL with a Different Value in MySQL

In MySQL, sometimes you don’t want NULL values to be returned as NULL. Sometimes you want NULL values to be returned with a different value, such as “N/A”, “Not Applicable”, “None”, or even the empty string “”.

Fortunately there are several ways to do this in MySQL.

Here are four:

  • The IFNULL() function
  • The COALESCE() function
  • The IF() function combined with the IS NULL (or IS NOT NULL) operator
  • The CASE expression combined with the IS NULL (or IS NOT NULL) operator

Examples of these options are below.

Continue reading

MySQL Group_Concat() vs T-SQL String_Agg()

One of the T-SQL functions introduced in SQL Server 2017 is the STRING_AGG() function. This is basically the equivalent of MySQL’s GROUP_CONCAT() function – it lets you return query results as a delimited list, rather than in rows.

But there are a few minor differences between the two functions.

This article explores some of the main syntax differences between these functions.

Continue reading

How to Return Query Results as a Comma Separated List in MySQL

In MySQL, you can return your query results as a comma separated list by using the GROUP_CONCAT() function.

The GROUP_CONCAT() function was built specifically for the purpose of concatenating a query’s result set into a list separated by either a comma, or a delimiter of your choice.

This article provides examples of how it all works.
Continue reading

MySQL COUNT() – Get the Number of Rows to be Returned by a Query

MySQL includes a COUNT() function, which allows you to find out how many rows would be returned from a query. This function is part of the SQL standard, and it can be used with most relational database management systems.

The COUNT() function can also be used to affect the results of a query if required, for example, by only returning those results that have a row count greater than a given amount.

This article contains examples of COUNT() usage in MySQL.

Continue reading

How to Check the Size of All Tables within a Database in MySQL

In MySQL, you can check the size of all tables within a given database (or on all databases) by querying the information_schema.tables table. This table stores data about each table in a database, including information about each table’s size, creation date, collation, etc.

You can also find the size of each table within a database by using the MySQL Workbench GUI.

This article provides a quick overview of each method.

Continue reading