Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer to Bit in SQL

I have an Integer field, and I need to divide it into Bits to the SQL(Firebird). For each byte of the Integer field should be a new field. For Example:

Integer field: 7 = 00000111

Bit 1 field1: 1  
Bit 2 field2: 1  
Bit 3 field3: 1  
Bit 4 Field4: 0
Bit 5 Field5: 0
Bit 6 Field6: 0
Bit 7 Field7: 0
Bit 8 Field8: 0

anyone know how to do this in Firebird?

like image 750
Paulo H. Hartmann Avatar asked Oct 22 '22 07:10

Paulo H. Hartmann


1 Answers

Use the BIN_AND function:

SELECT 
   bin_and(field, 1) as bit1, 
   bin_and(field, 2) as bit2, 
   bin_and(field, 4) as bit3,
   bin_and(field, 8) as bit4,
   ... 
FROM T
like image 127
ain Avatar answered Nov 01 '22 18:11

ain