In MySQL, the CHAR()
function returns the character for each integer passed. In other words, you can pass in one or more integers, and the function will interpret those as code values for string characters and return the corresponding string for each code value.
Tag: string functions
How the EXPORT_SET() Function Works in MySQL
The MySQL EXPORT_SET()
function returns a string that represents the bits in a number.
You have the ability to customise how the function outputs the string. The function accepts a number of arguments that allows you to do this. When you call the function, you provide the number in question, as well as arguments that determine how the results are displayed.
ORD() Examples – MySQL
In MySQL, the ORD()
function returns the numeric value of the leftmost character of a given string. You provide the string as an argument.
If the leftmost character is a multibyte character, the returned value is calculated from the numeric values of its constituent bytes. If the leftmost character is not a multibyte character, the return value is its ASCII code (which is the same result as when using the ASCII()
function).
ASCII() Examples – MySQL
In MySQL, the ASCII()
function returns the numeric ASCII code of the leftmost character of a given string. You provide the string as an argument.
This article contains examples of usage.
MySQL Group_Concat() vs T-SQL String_Agg()
One of the T-SQL functions introduced in SQL Server 2017 is the STRING_AGG()
function. This is basically the equivalent of MySQL’s GROUP_CONCAT()
function – it lets you return query results as a delimited list, rather than in rows.
But there are a few minor differences between the two functions.
This article explores some of the main syntax differences between these functions.
The SQL Server Equivalent to GROUP_CONCAT()
Before SQL Server 2017 came along, there wasn’t a T-SQL equivalent of the MySQL GROUP_CONCAT()
function. This function allows you to return a result set as a comma-separated list, as opposed to listing each row as a separate row (as with a normal result set).
Prior to SQL Server 2017, if you wanted to put your result into a comma separated list, you’d need to find a workaround, perhaps using a combination of STUFF()
and FOR XML PATH()
.
However, T-SQL now has the STRING_AGG()
function which is available from SQL Server 2017. This function does pretty much the same thing as MySQL’s GROUP_CONCAT()
function (with some minor differences).
How to Return Query Results as a Comma Separated List in SQL Server – STRING_AGG()
Starting with SQL Server 2017, you can now make your query results appear as a list. This means you can have your result set appear as a comma-separated list, a space-separated list, or whatever separator you choose to use.
While it’s true that you could achieve this same effect prior to SQL Server 2017, it was a bit fiddly.
Transact-SQL now has the STRING_AGG()
function, which concatenates the values of string expressions and places separator values between them. This works in much the same way to MySQL’s GROUP_CONCAT()
function.
This article provides examples that demonstrate the T-SQL STRING_AGG()
function.
How to Return Query Results as a Comma Separated List in MySQL
In MySQL, you can return your query results as a comma separated list by using the GROUP_CONCAT()
function.
The GROUP_CONCAT()
function was built specifically for the purpose of concatenating a query’s result set into a list separated by either a comma, or a delimiter of your choice.
This article provides examples of how it all works.
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.
2 Ways to Convert between Decimal and Hexadecimal in MySQL
This article presents two methods for converting a decimal number to its hexadecimal equivalent in MySQL.
More specifically, I present two functions that enable you to do this conversion. The two functions are as follows:
- The
HEX()
function - The
CONV()
function
Below is an explanation of each function.