Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Someway to do `where booleanvalue=false` on both Sql Server and PostgreSQL?

I am attempting to make an application capable of running on both Sql Server and PostgreSQL.

I can not seem to find a common expression that is basically

 select * from table where booleancol=false

on SQL Server I must do(which is very confusing because the default value for bit types must be true or false, but you can't assign them to true or false or test against it)

select * from table where booleancol=0

on PostgreSQL I must do

select * from table where booleancol is false

There are quite a lot of queries in our program that does this, so I'd prefer if there was just some universal syntax I could use instead of doing if(dbformat=="postgres").. type crap..

Also, I'd prefer to leave the columns as boolean/bit types and not change them to integer types.. though that is an option..

like image 444
Earlz Avatar asked Dec 22 '09 20:12

Earlz


People also ask

How does SQL Server store True False?

SQL Server Boolean A BIT data type is used to store bit values from 1 to 64. So, a BIT field can be used for booleans, providing 1 for TRUE and 0 for FALSE. CREATE TABLE testbool ( sometext VARCHAR(10), is_checked BIT ); This means you can insert either a 1 (for TRUE) or 0 (for FALSE) into this column.

Does PostgreSQL have the same syntax as SQL?

Despite the overwhelming popularity of MySQL, PostgreSQL may be a better choice because its syntax most closely conforms to Standard SQL. This means that you can easily translate your skills to other database management systems such as MySQL or SQLite.

How do you get true or false in SQL?

SQL Server does not have the Boolean data type. There are no built-in values true and false . One alternative is to use strings 'true' and 'false' , but these are strings just like any other string. Often the bit type is used instead of Boolean as it can only have values 1 and 0 .


1 Answers

Sorry, this part is simply not true; less than half-true ;-)

on PostgreSQL I must do

select * from table where booleancol is false

In fact, all following syntaxes are valid in PostgreSQL:

select * from table where not booleancol
select * from table where booleancol = 'f'
select * from table where booleancol = 'false'
select * from table where booleancol = 'n'
select * from table where booleancol is false
select * from table where booleancol is not true
select * from table where booleancol = false
select * from table where booleancol <> true
select * from table where booleancol != true
select * from table where booleancol <> 'TRUE'

That's example of postgres flexible syntax, and it makes porting apps from other databases quite easy.

See the docs.

like image 108
filiprem Avatar answered Oct 21 '22 04:10

filiprem