MySQL EXPORT_SET function is one of the String Function, which returns a string using the separator, on and Off arguments, bit value, and the number of bits.
Let me show you how to write MySQL String EXPORT_SET Function query to return a string using bit values and number of bits with an example.
MySQL EXPORT_SET Syntax
The basic syntax of EXPORT_SET of a string in MySQL is as shown below:
EXPORT_SET(bits, On_Str, Off_Str, Separator, Number of Bits)
- Bits: It converts the given value to binary value or Bits.
- On_Str and Off_Str: If the first argument bit value is 1, then this is returned. Otherwise, Off_String value returned.
- Number of Bits: Total number of bits to compare.
MySQL EXPORT_SET Example
The MySQL string EXPORT_SET function returns a string using the bit values, On and Off string with separators. The following query shows multiple ways to use this EXPORT_SET function.
Here, we take the first statement for the analysis purpose. bits = 5 = 0101. As we said earlier, it starts from right to left. So, the first bit is 1, and it returns On_String A. The next one is 0, so it returns Off_String B. Do the same for the remaining two.
In the second statement, we used the number of bits value to three. It means, 101 so, the output will be A, B, A
SELECT EXPORT_SET(5, 'A', 'B', ',', 4);
SELECT EXPORT_SET(5, 'A', 'B', ',', 3);
SELECT EXPORT_SET(4, '1', '0', ',', 8);
In this MySQL Export_Set example, we are using large numbers as a bit argument value. Here, we also used the $ as the separator.
SELECT EXPORT_SET(39, '1', '0', ',', 5);
SELECT EXPORT_SET(245, '1', '0', '$', 16);
SELECT EXPORT_SET(24500, '1', '0', ',', 32);