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

AVG() – Calculate the Average Value of a Column in MySQL

When using MySQL, you can use the AVG() function to calculate the average value from a range of values.

For example, you can use this function to find out what the average city population is for a given country or state. Given a country will have many cities, each with different populations, you can find out what the average is between them. One city might have a population of say, 50,000 while another has a population of 500,000. The AVG() function will calculate the average for you.

Continue reading

How to Convert a String to Hexadecimal in MySQL – HEX()

In MySQL, you can convert a string to its hexadecimal representation by using the HEX() function.

The HEX() function works on both strings and numbers, however, its handling of each of these is slightly different. When used to convert a number, it returns a hexadecimal string representation of the number treated as a longlong (BIGINT) number. But when converting a string, it returns a hexadecimal string representation of the string where each byte of each character is converted to two hexadecimal digits.

Continue reading

3 Ways to “Unhex” a String in MySQL

MySQL includes various functions that can assist you when working with hexadecimal values. One of the most obvious functions is the HEX() function, which enables you to convert a string into its hexadecimal equivalent.

However, there may be times where you want to convert a hexadecimal string back to a more readable string. That’s what this article is about.

Here are three ways to “unhex” a string in MySQL:

  • The UNHEX() function
  • The X hexadecimal notation
  • The 0x notation

Below are examples of each of these methods.

Continue reading

How to “Unhex” a Number in MySQL

In MySQL, you can unhex a string using the UNHEX() function. But you can’t unhex a number with that function.

To unhex a number in MySQL, use the CONV() function instead.

The CONV() function allows you to convert numeric values between different numbering systems. For example, you can convert between say decimal and binary, octal to decimal, or, more relevant to this article, between hexadecimal and decimal.

Continue reading

How to Use STRCMP() to Compare 2 Strings in MySQL

One of the string functions in MySQL is STRCMP().  This function allows you to compare two strings according to their sort order.

The function accepts two arguments. Each argument is the string to compare. It returns either 1, -1, or 0, depending on whether the first string is larger, smaller, or the same size as the second string, according to sort order.

Continue reading

MySQL SOUNDEX() Examples

One of the many MySQL string functions is the SOUNDEX() function. This function returns a Soundex string from a given string. If two words sound the same, they should have the same Soundex string. If two words sound similar, but not exactly the same, their Soundex string might look similar but not exactly the same.

This article contains a bunch of Soundex examples to demonstrate how the SOUNDEX() function works in MySQL.

Continue reading

2 Ways to Convert a Number to Octal in MySQL

If you need to convert a number from decimal to octal (base 8), two functions come to mind if you’re using MySQL. One function is specifically for doing octal conversions, the other is for doing conversions between different bases. These are as follows:

OCT()
This function is used specifically for converting from decimal to octal.
CONV()
This function has a more general purpose. It allows you to specify the base of the original number and the result. In other words, you can convert from any base, to any base (as long as each base is between 2 and 36).

More about these two functions below.

Continue reading

How to Pad a String with Leading/Trailing Characters in MySQL – LPAD(), RPAD()

Sometimes you need to pad a string with spaces. Or perhaps you’ll pad it with another character. Sometimes you need to pad it on the left. Other times you need to pad it on the right. Or maybe you need to pad it on both sides.

All of the above can be done in MySQL using the LPAD() and/or RPAD() string functions.

Continue reading