MySQL | EXPORT_SET method
Start your free 7-days trial now!
MySQL's EXPORT_SET(~)
method returns a string such that for every set bit (bit value 1), we append the on
string and for every unset bit (bit value 0), we append the off
string.
Parameters
1. bits
| number
A value whose binary determines the order of appending on
and off
strings to the return string.
2. on
| string
The string to be appended for bit value 1.
3. off
| string
The string to be appended for bit value 0.
4. separator
| string
| optional
The separator for the substrings in the return string. Defaults to comma.
5. number_of_bits
| integer
| optional
The number of bits examined. Defaults to 64.
Examples
Basic usage
To return a string based on bits
arguments 4
:
SELECT EXPORT_SET(4, 'Y', 'N', ',', 3);
+---------------------------------+| EXPORT_SET(4, 'Y', 'N', ',', 3) |+---------------------------------+| N,N,Y |+---------------------------------+
Representing 4
in binary is 100
, hence we now know the order in which on
and off
strings should be appended to the return string. Reading the binary from the right:
Bit # | Bit value | Append to the return string |
---|---|---|
First bit | 0 |
|
Second bit | 0 |
|
Third bit | 1 |
|
Therefore, we see the final returned string of 'N,N,Y'
where the substrings are comma separated.
Separator parameter
To use pipe symbol |
as separator for substrings in return string:
SELECT EXPORT_SET(4, 'Y', 'N', '|', 3);
+---------------------------------+| EXPORT_SET(4, 'Y', 'N', '|', 3) |+---------------------------------+| N|N|Y |+---------------------------------+
Number of bits parameter
The default number of bits examined is 64:
SELECT EXPORT_SET(4, 'Y', 'N', ',');
+---------------------------------------------------------------------------------------------------------------------------------+| EXPORT_SET(4,'Y','N',',') |+---------------------------------------------------------------------------------------------------------------------------------+| N,N,Y,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N |+---------------------------------------------------------------------------------------------------------------------------------+
Earlier we stated the binary of 4
was 100
. However, when this is represented in 64 bits, 100
is left padded with zeros until 64 bits instead of the current 3 bits. Hence binary of 4
using 64 bits looks like:
0000000000000000000000000000000000000000000000000000000000000100
This is why we see many 'N'
s appended to the result string when we do not specify the number of bits examined.