How to Concatenate Strings in SQL Server with CONCAT()

In SQL Server, you can concatenate two or more strings by using the T-SQL CONCAT() function. You can also use SQL Server’s string concatenation operator (+) to do the same thing. Both are explained here.

In SQL Server (and in any computer programming environment), string concatenation is the operation of joining character strings end-to-end.

Here’s an example:

SELECT CONCAT('Peter', ' ', 'Griffin') AS 'Full Name';

Result:

Full Name    
-------------
Peter Griffin

Note that I actually concatenated 3 strings here. I concatenated the first name, the last name, plus a space.

Continue reading

How to Concatenate Strings in MySQL with CONCAT()

MySQL has the CONCAT() function, which allows you to concatenate two or more strings. The function actually allows for one or more arguments, but its main use is to concatenate two or more strings.

In MySQL (and in any computer programming environment), string concatenation is the operation of joining character strings end-to-end.

Here’s an example:

SELECT CONCAT('Homer', ' ', 'Simpson') AS 'Full Name';

Result:

+---------------+
| Full Name     |
+---------------+
| Homer Simpson |
+---------------+

Note that I actually concatenated 3 strings here. I concatenated the first name, the last name, plus a space.

Continue reading

What’s the DATALENGTH() Equivalent in MySQL?

If you’ve been working with SQL Server for any length of time, you might have encountered the Datalength() function. This function returns the number of bytes used to represent an expression.

But if you’ve moved to MySQL, you may be looking for a function that does the same thing.

In MySQL, the Length() function does basically the same thing that the T-SQL Datalength() function does in SQL Server (and Azure). MySQL’s Length() function returns the length of a string, measured in bytes.
Continue reading

MySQL length() vs char_length()

In MySQL, there are many times where the length() function and the char_length() function will provide exactly the same results. However, there are also times where the results will be completely different. Here’s why.

First, here’s the definition for each of these functions:

char_length()
Returns the length of a string, measured in characters.
length()
Returns the length of a string, measured in bytes.

Notice “characters” vs “bytes” – one is measured in characters, the other is measured in bytes.

In many cases, the number of bytes will be the same as the number of characters in the string, but this isn’t always the case. The number of bytes used per character depends on how the data is stored. For example, if the string is stored as Unicode data, there will be 2 bytes per character.
Continue reading

What is LENGTH() in MySQL?

One of the many functions in MySQL is the LENGTH() function, which returns the length of a string, measured in bytes.

Example:

SELECT LENGTH('Lit');

Result:

+---------------+
| LENGTH('Lit') |
+---------------+
|             3 |
+---------------+

This is a simple example and the result is the same as if we’d used the CHAR_LENGTH() function. However, the LENGTH() function can return different results, depending on the data type.
Continue reading

LEN() vs DATALENGTH() in SQL Server

When using T-SQL in SQL Server (or Azure) the LEN() and DATALENGTH() functions will often return the same result, but not always. There are some cases where these functions will return completely different results for what appears to be the same data. This is because there’s an important difference between how the LEN() and DATALENGTH() functions work, as we’ll see here.

First up, here’s a quick definition of each:

LEN()
Returns the number of characters of the specified string expression, excluding trailing blanks.
DATALENGTH()
Returns the number of bytes used to represent any expression.

Note “characters” vs “bytes”. Also note that “excluding trailing blanks” only applies to one.

Here are some examples to demonstrate the differences between LEN() and DATALENGTH().
Continue reading