无论如何在Sql Server和PostgreSQL上执行`where booleanvalue = false`?

Ear*_*rlz 17 sql-server postgresql compatibility ansi-sql

我试图使一个应用程序能够在Sql Server和PostgreSQL上运行.

我似乎无法找到一个基本的共同表达

 select * from table where booleancol=false
Run Code Online (Sandbox Code Playgroud)

在SQL Server上我必须做(这是非常令人困惑的,因为位类型的默认值必须为true或false,但你不能将它们分配为true或false或对它进行测试)

select * from table where booleancol=0
Run Code Online (Sandbox Code Playgroud)

在PostgreSQL上我必须这样做

select * from table where booleancol is false
Run Code Online (Sandbox Code Playgroud)

在我们的程序中有很多查询都这样做,所以我更喜欢是否只有一些通用语法我可以使用而不是做if(dbformat=="postgres")..类型废话..

此外,我更喜欢将列保留为布尔/位类型,而不是将它们更改为整数类型..虽然这是一个选项..

fil*_*rem 30

对不起,这部分根本不是真的; 不到一半真实;-)

在PostgreSQL上我必须这样做

select * from table where booleancol is false

实际上,以下所有语法在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'
Run Code Online (Sandbox Code Playgroud)

这是postgres灵活语法的例子,它使得从其他数据库移植应用程序非常容易.

查看文档.


Not*_*tMe 17

SQL Server会自动将位值更改为varchar值true或false.所以以下是有效的:

select * from table where booleancol = 'false'
Run Code Online (Sandbox Code Playgroud)

我不知道postgre是否做同样的事情.