PostgreSQL。创建演员表“角色变化”为“整数”

发布于 2024-10-13 05:35:10 字数 55 浏览 2 评论 0原文

我想创建一个合适的函数来将“字符变化”转换为“整数”。谁能建议一个功能吗?我尝试的一切都失败了。

I want to CREATE CAST a suitable function to convert 'character varying' to 'integer'. Can anyone suggest a function? Everything I try fails.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

皇甫轩 2024-10-20 05:35:10

使用它:

CREATE CAST (varchar AS integer) WITH INOUT [AS IMPLICIT];

它使用所涉及的数据类型的输入/输出函数。

Use this:

CREATE CAST (varchar AS integer) WITH INOUT [AS IMPLICIT];

It uses the input/output functions of the data types involved.

瑶笙 2024-10-20 05:35:10

如果您更新 SQL 以执行显式转换,则上面的函数将起作用。
但是,如果您将“asimplicit”添加到“createcast”语句的末尾,当您将整数与可转换为整数的varchar进行比较时,Postgres将能够自动找出您要执行的操作。

这是我更新的声明,它似乎“隐式”起作用:

CREATE FUNCTION toint(varchar) 
  RETURNS integer 
  STRICT IMMUTABLE LANGUAGE SQL AS 
'SELECT cast($1 as integer);';

CREATE CAST (varchar AS integer) WITH FUNCTION toint(varchar) as Implicit;

The function above works if you update your SQL to do an explicit cast.
However, if you add 'as implicit' to the end of your "create cast" statement, Postgres will be able to automatically figure out what you are trying to do when you compare an integer with a varchar that can be converted to an integer.

Here is my updated statement that seemed to work "implicitly":

CREATE FUNCTION toint(varchar) 
  RETURNS integer 
  STRICT IMMUTABLE LANGUAGE SQL AS 
'SELECT cast($1 as integer);';

CREATE CAST (varchar AS integer) WITH FUNCTION toint(varchar) as Implicit;
情痴 2024-10-20 05:35:10

我假设您想恢复 Postgres 8.3 关于隐式转换的行为。

请参阅此博客条目以获取示例:
http://petereisentraut.blogspot.com/2008/03 /readding-implicit-casts-in-postgresql.html

哦:还有软件供应商修复损坏的 SQL 的 bug ;)

编辑

这应该可以做到:

CREATE FUNCTION toint(varchar) 
  RETURNS integer 
  STRICT IMMUTABLE LANGUAGE SQL AS 
'SELECT cast($1 as integer);';

CREATE CAST (varchar AS integer) WITH FUNCTION toint(varchar);

I assume you want to get back the behaviour of Postgres 8.3 regarding implicit casting.

See this blog entry for examples:
http://petereisentraut.blogspot.com/2008/03/readding-implicit-casts-in-postgresql.html

Oh: and bug that vendor of the software to fix the broken SQL ;)

Edit

This should do it:

CREATE FUNCTION toint(varchar) 
  RETURNS integer 
  STRICT IMMUTABLE LANGUAGE SQL AS 
'SELECT cast($1 as integer);';

CREATE CAST (varchar AS integer) WITH FUNCTION toint(varchar);
滿滿的愛 2024-10-20 05:35:10

我遇到了同样的错误。我有一个专栏

代码

声明为类型字符变化(20)和局部变量

l_代码

声明为 int 类型。

我解决了在程序中替换

SELECT 
...
WHERE
code = l_code AND
..

code = cast( l_code as character varying)   AND

I got the same error. I have a COLUMN

code

declared as type character varying(20) and a local variable

l_code

declared as type int.

I solved replacing in the procedure

SELECT 
...
WHERE
code = l_code AND
..

with

code = cast( l_code as character varying)   AND
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文