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

How the SQL Server DIFFERENCE() Function Works

This article aims to help you understand the DIFFERENCE() function, which is a T-SQL function available in SQL Server, Azure, etc.

The key to understanding the DIFFERENCE() function is to understand how Soundex works (or in the context of SQL Server, how the SOUNDEX() function works). This is because the DIFFERENCE() function returns the difference between the Soundex values between two strings.

A Soundex value is four characters long. If two words sound the same, they will share the same four character Soundex value. If they sound similar (but not the same), their Soundex values might share some characters but not all. For example, their Soundex values might have two characters the same and two that are different. If two words sound completely different, none of the characters in their respective Soundex values will be the same.

The DIFFERENCE() function returns a value that ranges from 0 through 4.  This value represents the number of characters in the Soundex values that are the same. 0 indicates weak or no similarity, and 4 indicates strong similarity or the same values.

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

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