Link to home
Start Free TrialLog in
Avatar of Stuart Landreth
Stuart LandrethFlag for United Kingdom of Great Britain and Northern Ireland

asked on

MySQL Bit comparison (days of week)

This has surely got to be an easy thing to accomplish.

I want to compare two bit strings (in this case representing days of the week) to see if they match, e.g.
1111000 AND 0000100 = 0
1111100 AND 0000100 = 1
The output should be 1 or 0

When I try this in MySQL
SELECT 1111000 AND 0000100 seems to give 1
SELECT 1111000 & 0000100 gives an integer output.

How do I do it?
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bitwise AND of 1111000 and 0000100 is 0, but bitwise AND of 1111100 and 0000100 is 0000100 which has a decimal value of 4. If you just want to compare two strings, you could use strcmp (https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#function_strcmp) which "returns 0 if the strings are the same, -1 if the first argument is smaller than the second according to the current sort order, and 1 otherwise."
Avatar of Stuart Landreth

ASKER

SELECT b'1111000' & b'0000100' > 0 gives the exact answer but this is the closest solution.