在postgresql 9.3中有什么技术可做,以便它开始返回1和0而不是“ t”。和“ f”表示对于布尔类型 [英] Is there any technique to do in postgresql 9.3 so that it start returning 1 and 0 instead of "t" and "f" for boolean type

查看:38
本文介绍了在postgresql 9.3中有什么技术可做,以便它开始返回1和0而不是“ t”。和“ f”表示对于布尔类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我正在将我的ms sql服务器迁移到postgresql 9.3中,我们使用int数据类型存储布尔值的1和0,但是在postgresql9.3中是不可能的。我对Postgresql的数据类型进行了一些研究后得到了什么:

Actually i am migrating my ms sql server into postgresql 9.3 and we were using int data type to store 1 and 0 for boolean values but in postgresql9.3 it is not possible. what did i get when i did a little research on datatype of postgresql:

In Postgresql:-
For Integer type datatype:-

insert into ask(aint) values(1)         working
insert into ask(aint) values('1')       working

insert into ask(aint) values(true)      not working

select * from ask where aint=1;     working
select * from ask where aint='1';   working
select * from ask where aint=true;  working


*For smallint type datatype:-

insert into ask(asmall) values(1);      working
insert into ask(asmall) values('1');    working
insert into ask(asmall) values(true);   working

select * from ask where asmall = 1      working
select * from ask where asmall = '1'    working
select * from ask where asmall = true   not working

所以如果我要使用smallint,那么我遇到的问题是,我无法比较任何布尔字段和如果我使用整数,那么即使我可以比较booelan字段,我也不能插入任何布尔值。

so if i want to use smallint then the problem that i am getting is , i can not compare any boolean field and if i use integer then i can not insert any boolean value eventhough i can compare booelan field. And

我不能使用布尔数据类型,因为它返回 t为真, f为假
,所以我想要的是什么?

i can not use boolean data type as it return "t" for true and "f" for false so what i want is?


  1. 是否有任何方法可以将smallint与布尔值进行比较?

  2. 还是有任何方法可以在其中插入布尔值整数类型数据类型。

  3. 或者我们可以做些什么,使布尔类型开始为true返回1,为false返回0。

那么有人可以帮我吗?紧急!谢谢

So can somebody please help me? it's emergency! thanks

推荐答案

默认情况下,没有这种转换-默认情况下,隐式转换仅用于相似组中的数据类型(例如整数 bigint )。

By default, there is no such conversion - implicit conversion is by default used only for data types from similar groups (e.g. integer and bigint).

幸运的是,您可以

首先,我们将创建一个新函数来转换 smallint boolean 。为此,我们将使用现有的C函数 bool_int4

First, we will create a new function to convert smallint to boolean. To do this, we will use existing C function bool_int4.

CREATE OR REPLACE FUNCTION int2(boolean)
  RETURNS smallint AS
'bool_int4'
  LANGUAGE internal IMMUTABLE STRICT
  COST 1;

现在我们可以创建从boolean到smallint的新隐式强制转换:

Now we can create new implicit cast from boolean to smallint:

CREATE CAST (boolean AS smallint) WITH FUNCTION int2(boolean) as implicit;

最后,我们准备直接比较smallint和boolean:

And finally, we are ready to compare smallint and boolean directly:

select 1::smallint=true

PostgreSQL 文档警告所有人,当您使用时会发生坏事明智地使用隐式强制转换:

PostgreSQL documentation warns everyone that bad things can happen when you use implicit casting unwisely:


保守地将强制转换标记为隐式是明智的。
过多的隐式转换路径可能导致PostgreSQL选择
令人惊讶的命令解释,或者因为存在多种可能的解释而根本无法解析
命令。
的一个好的经验法则是仅对同一
一般类型类别中的类型之间的
信息保留转换进行隐式调用。

It is wise to be conservative about marking casts as implicit. An overabundance of implicit casting paths can cause PostgreSQL to choose surprising interpretations of commands, or to be unable to resolve commands at all because there are multiple possible interpretations. A good rule of thumb is to make a cast implicitly invokable only for information-preserving transformations between types in the same general type category.

您可以从表中获取指定类型的强制类型转换列表。 postgresql.org/docs/current/static/catalog-pg-cast.html rel = nofollow> pg_cast

You can get list of casts for specified types from pg_catalog, from table pg_cast:

select 
  castsource::regtype,
  casttarget::regtype,
  castcontext
from 
  pg_catalog.pg_cast
where
  castsource::regtype in ('boolean','smallint','integer') and
  casttarget::regtype in ('boolean','smallint','integer')
order by
  castsource::regtype,
  casttarget::regtype;

这将返回(9.1.9):

This will return (9.1.9):

castsource;casttarget;castcontext;
boolean;integer;e
smallint;integer;i
integer;boolean;e
integer;smallint;a

这篇关于在postgresql 9.3中有什么技术可做,以便它开始返回1和0而不是“ t”。和“ f”表示对于布尔类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆