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

CONVERT() vs TRY_CONVERT in SQL Server: What’s the Difference?

You might have noticed that T-SQL includes both a CONVERT() function and a TRY_CONVERT() function that you can use in SQL Server to perform conversions between data types. But if you’re scratching your head over what the difference is between these two functions, read on!

The difference between CONVERT() and TRY_CONVERT() is in the way they handle data types that can’t be converted. One throws an error, while the other returns null. The following examples demonstrate this.

Continue reading

CAST() vs TRY_CAST() in SQL Server: What’s the Difference?

When converting between data types in SQL Server, you will run into various functions that seemingly do the same thing. However, there are usually differences between these functions that might not be apparent at first glance. An example of this is the difference between the CAST() and TRY_CAST() functions.

This article demonstrates the difference between these functions when using SQL Server.

Continue reading

6 Ways to Concatenate a String and a Number in SQL Server

If you’ve ever tried to concatenate a string with a number while using SQL Server, but received an error, this article should clear things up for you. There’s more than one way to perform concatenation using T-SQL in SQL Server, and if you’re concatenating different data types (like a string and a number) then you may receive an error, depending on how you do the concatenation.

The thing to remember when concatenating different data types is that they need to be converted into the same data type first. More specifically, when concatenating a string with a number, the number will need to be converted to a string before it can be concatenated with the string. Fortunately SQL Server/T-SQL makes this a breeze.

This article presents six ways to concatenate strings with numbers using T-SQL.

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

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