skip to Main Content

I have a bit column (for example, ‘Column’).

How can I compare the value of this column to just any number?

SELECT * FROM TABLE WHERE 'Column' = 5

After executing the above query, I get an error message: the operator does not exist: bit = integer

2

Answers


  1. When comparing a bit field to a number in PostgreSQL, you should use the bit literal syntax. You can alter your query as follows to compare the value 5 to a bit column named "Column":

    SELECT * FROM your_table_name WHERE "Column" = B'101'; -- B'101' represents the bit value 5
    

    This query will produce a list of the rows where the ‘Column’ field’s bit representation is 5.

    Hope it works 🙂

    Login or Signup to reply.
  2. You can convert the value to compre to bit

    CREATE TABLE TABLE_1 (Column1 bit(4))
    
    CREATE TABLE
    
    SELECT * FROM TABLE_1 WHERE Column1 = 5::BIT(4)
    
    column1
    SELECT 0
    

    fiddle

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search