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

Left Padding in SQL Server – 3 LPAD() Equivalents

If you use Oracle Database or MySQL, you’re lucky enough to have the LPAD() and RPAD() functions, which allow you to pad a string with a given character (or characters) to its left and/or right.

However, SQL Server (or more precisely, T-SQL), doesn’t include these functions. So if you need some left padding, you’ll need to improvise.

This article presents four options for padding a number with leading zeros in SQL Server. So you can do stuff like turn 7 into 007. Three of these options work on strings, so you can also apply padding to textual data.

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

How to Add Leading Zeros to a Number in MySQL

When working with MySQL, you might occasionally encounter situations where you need to pad a bunch of numbers with leading zeros.

Perhaps you have a requirement that all numbers have three digits, but in the data you’ve been supplied, the numbers range from a single digit, to two, and maybe three digits. Your requirement might be to pad all numbers with leading zeros, but only to make up for any shortfall in the three digit requirement.

The LPAD() function does exactly what you want in this scenario.

Continue reading

How to Retain the Backslash when Escaping Quotes in MySQL – QUOTE()

Using a backslash to escape single quotes is a commonly used technique to ensure that the single quotes don’t interfere with MySQL‘s handling of the full string.

Single quotes are used to surround a string, so a single quote within the string itself could cause havoc if it isn’t properly escaped. Simply inserting a backslash immediately before the quote mark ensures that MySQL doesn’t terminate the string prematurely.

However, there may be occasions where you want the backslash to remain. For example, you might intend to use the string in an SQL statement, and therefore, you want it to be properly escaped first.

This is where the QUOTE() function comes in.

The MySQL QUOTE() function quotes a string to produce a result that can be used as a properly escaped data value in an SQL statement. The string is returned enclosed by single quotation marks and with each instance of backslash (\), single quote ('), ASCII NUL, and Control+Z preceded by a backslash.

Continue reading