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

How to Replace NULL with Another Value in SQL Server – ISNULL()

When querying a SQL Server database, there may be times where you don’t want null values to be returned in your result set. And there may be times where you do want them returned. But there may also be times where you do want them returned, but as a different value.

That’s what the ISNULL() function is for.

ISNULL() is a T-SQL function that allows you to replace NULL with a specified value of your choice.

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

The SQL Server Equivalent to GROUP_CONCAT()

Before SQL Server 2017 came along, there wasn’t a T-SQL equivalent of the MySQL GROUP_CONCAT() function. This function allows you to return a result set as a comma-separated list, as opposed to listing each row as a separate row (as with a normal result set).

Prior to SQL Server 2017, if you wanted to put your result into a comma separated list, you’d need to find a workaround, perhaps using a combination of STUFF() and FOR XML PATH().

However, T-SQL now has the STRING_AGG() function which is available from SQL Server 2017. This function does pretty much the same thing as MySQL’s GROUP_CONCAT() function (with some minor differences).

Continue reading

How to Return Query Results as a Comma Separated List in SQL Server – STRING_AGG()

Starting with SQL Server 2017, you can now make your query results appear as a list. This means you can have your result set appear as a comma-separated list, a space-separated list, or whatever separator you choose to use.

While it’s true that you could achieve this same effect prior to SQL Server 2017, it was a bit fiddly.

Transact-SQL now has the STRING_AGG() function, which concatenates the values of string expressions and places separator values between them. This works in much the same way to MySQL’s GROUP_CONCAT() function.

This article provides examples that demonstrate the T-SQL STRING_AGG() function.

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 Return the Number of Rows in a Query Result in SQL Server

When working with databases, sometimes you want to find out how many rows will be returned by a query, without actually returning the results of the query. Or sometimes you might just want to find out how many rows are in a given table.

In SQL Server, you can use T-SQL‘s COUNT() function to return the number of rows that would be returned in a query.

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

How to Check the Size of a Database in MySQL

In MySQL, you can query the information_schema.tables table to return information about the tables in a database. This table includes information about the data length, index length, as well as other details such as collation, creation time, etc. You can use the information in this table to find the size of a given database or all databases on the server.

You can also use the MySQL Workbench GUI to find details about the database (including its size).

This article provides a quick overview of both methods.

Continue reading