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

泪湿孤枕 提交于 2019-12-04 22:49:58

SQL Server will automatically change the bit value to the varchar value of true or false. So the following works there:

select * from table where booleancol = 'false'

I have no idea if postgre does the same thing.

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.

At work, we use 'T' and 'F' in char(1) columns to represent booleans in SQL Server. I'm not sure if this sort of compatibility was the reason, but it does mean that "booleancol = 'F'" would work on either flavour of database.

If it's an option, take a look at an Object Relational Mapping framework, which could handle the problem of translating SQL from one RDBMS to another.

Use an ORM, there is no need today to hand code SQL. They exist so solve the very problem you have run into.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!