SQL 在 Select (PostgreSQL/pgAdmin) 中返回布尔值作为 TRUE 或 FALSE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34440068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return Boolean Value as TRUE or FALSE in Select (PostgreSQL/pgAdmin)
提问by Net Dawg
In PostgreSQL (version 9.4, pgAdmin3), when doing select on a table with boolean column the data output shows 't' or 'f'. I would like to cast/convert booleans as TRUE or FALSE without writing CASE statements or doing JOINS etc.
在 PostgreSQL(版本 9.4,pgAdmin3)中,当对带有布尔列的表进行选择时,数据输出显示 't' 或 'f'。我想在不编写 CASE 语句或执行 JOINS 等的情况下将布尔值转换/转换为 TRUE 或 FALSE。
BTW, according to PostgreSQL own documentationthis behavior is not the SQL standard.
顺便说一句,根据 PostgreSQL 自己的文档,这种行为不是 SQL 标准。
The key words TRUE and FALSE are the preferred (SQL-compliant) usage.
关键字 TRUE 和 FALSE 是首选(符合 SQL 的)用法。
PS: This happens only when using the SQL Editor in pgAdmin. Use pgAdmin object browser, drill down to same table, right-click, view data, View Top 100 rows, the same boolean column shows up as TRUE or FALSE, as expected/standard.
PS:只有在 pgAdmin 中使用 SQL 编辑器时才会发生这种情况。使用 pgAdmin 对象浏览器,向下钻取到同一个表,右键单击,查看数据,查看前 100 行,同一个布尔列显示为 TRUE 或 FALSE,如预期/标准。
采纳答案by zedfoxus
If all you want to show is the literal TRUE
or FALSE
, you can use the case statements like you had proposed. Since PostgreSQL treats TRUE
, true
, yes
, on
, y
, t
and 1
as true, I'd control how I'd want the output to look like.
如果您只想显示文字TRUE
or FALSE
,则可以使用您建议的 case 语句。由于 PostgreSQL 将TRUE
, true
, yes
, on
, y
,t
和1
视为真,我会控制我希望输出的样子。
Where clause can be written like:
Where 子句可以写成:
select * from tablename where active
--or--
select * from tablename where active = true
(My recommendation is the same as PostgreSQL - use true)
(我的建议和 PostgreSQL 一样——使用 true)
When selecting, although there may be hesitation to use the case statements, I'd still recommend doing that to have control over your output string literal.
在选择时,虽然可能会犹豫使用 case 语句,但我仍然建议这样做以控制您的输出字符串文字。
Your query would look like this:
您的查询将如下所示:
select
case active = TRUE then 'TRUE' else 'FALSE' end as active_status,
...other columns...
from tablename
where active = TRUE;
SQLFiddle example: http://sqlfiddle.com/#!15/4764d/1
SQLFiddle 示例:http://sqlfiddle.com/#!15/4764d/1
create table test (id int, fullname varchar(100), active boolean);
insert into test values (1, 'test1', FALSE), (2, 'test2', TRUE), (3, 'test3', TRUE);
select
id,
fullname,
case when active = TRUE then 'TRUE' else 'FALSE' end as active_status
from test;
| id | fullname | active_status |
|----|----------|---------------|
| 1 | test1 | FALSE |
| 2 | test2 | TRUE |
| 3 | test3 | TRUE |
回答by Erwin Brandstetter
A simple cast to text
will do the job (unless you need upper case spelling):
一个简单的演员就text
可以完成这项工作(除非你需要大写拼写):
SELECT true::text AS t, false::text AS f;
t | f
------+-------
true | false
Else, the text representation depends on library and client you use to connect. JDBC for instance renders boolean
values as 'true' / 'false' anyway:
否则,文本表示取决于您用于连接的库和客户端。例如,JDBCboolean
无论如何将值呈现为“真”/“假”:
You will love this change in Postgres 9.5(quoting the release notes):
你会喜欢 Postgres 9.5 中的这个变化(引用发行说明):
Use assignment cast behavior for data type conversions in PL/pgSQL assignments, rather than converting to and from text (Tom Lane)
This change causes conversions of Booleans to strings to produce
true
orfalse
, nott
orf
. Other type conversions may succeed in more cases than before; for example, assigning a numeric value3.9
to an integer variable will now assign4
rather than failing. If no assignment-grade cast is defined for the particular source and destination types, PL/pgSQL will fall back to its old I/O conversion behavior.
对 PL/pgSQL 赋值中的数据类型转换使用赋值强制转换行为,而不是在文本之间进行转换(Tom Lane)
此更改导致将布尔值转换为字符串以生成
true
orfalse
、nott
orf
。其他类型转换可能在比以前更多的情况下成功;例如,将数值分配给3.9
整数变量现在将分配4
而不是失败。如果没有为特定的源和目标类型定义赋值等级转换,PL/pgSQL 将回退到其旧的 I/O 转换行为。
Bold emphasis mine.
大胆强调我的。