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..
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.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With