将包含一些字符串的 varchar 转换为 informix 中的整数字段

Cast varchar that holds some strings to integer field in informix

我想比较数据库中 2 个表中的 2 行。

Column1 在 table1 上,是一个整数字段,包含如下条目

column1 147518 187146 169592

Column2 在 table2 上,是一个具有各种条目的 Varchar(15) 字段,但对于此示例,我们使用这些 3:

column2 169592 00010000089 DummyId

我的查询部分依赖于检查表 1 中的行是否链接到表 2 中的行,但为此,我需要比较列 1 和列 2。

SELECT * FROM table1 WHERE column1 IN (SELECT column2 FROM table2)

使用上述数据的结果应该是 1 行 - 169592

显然这行不通(字符到数字的转换过程失败),因为它们无法按原样进行比较,但我如何让它们工作?

我试过了

SELECT * FROM table1 WHERE column1 IN (SELECT CAST(column2 AS INTEGER) FROM table2)

SELECT * FROM table1 WHERE column1 IN (SELECT (column2::INTEGER) column2 FROM table2)

如果有帮助,请使用 Server Studio 9.1。

您可以尝试在下面使用ISNUMERIC

SELECT * FROM table1 WHERE column1 IN (SELECT CASE WHEN ISNUMERIC(column2) = 1 THEN CAST(column2 AS INT) END FROM table2)

尝试将整数转换为字符串:

SELECT * FROM table1 WHERE cast(column1 as varchar(15)) IN (SELECT column2 FROM table2)

@Stanislovas Kalašnikovas 回答了这个问题的一部分,他说使用以下内容:

SELECT * FROM table1 WHERE column1 IN (SELECT CASE WHEN ISNUMERIC(column2) = 1 THEN CAST(column2 AS INT) END FROM table2)

但是 informix 没有 ISNUMERIC 的内置函数,所以下面创建了它:

create function isnumeric2(inputstr varchar(15)) returning integer;
define numeric_var decimal(15,0);
define function_rtn integer;

on exception in (-1213)
let function_rtn = 0;
end exception with resume

let function_rtn = 1;
let numeric_var = inputstr;

return function_rtn;
end function;

然后上面的第一个查询对我有用。

为此,无需创建您在其他环境中找不到的特殊功能。

让我们为您的示例创建一个测试用例:

CREATE TABLE tab1 (
        col1 INT,
        col2 INT
);

CREATE TABLE tab2 (
        col1 VARCHAR(15)
);

INSERT INTO tab1 VALUES(147518,1);
INSERT INTO tab1 VALUES(187146,2);
INSERT INTO tab1 VALUES(169592,3);

INSERT INTO tab2 VALUES(169592);
INSERT INTO tab2 VALUES('00010000089');
INSERT INTO tab2 VALUES('DummyId');

您 运行 的第一个查询是这样的:

SELECT  t1.*
FROM    tab1 AS t1
WHERE   t1.col1 IN (SELECT t2.col1 FROM tab2 AS t2);

这会引发错误,因为它试图将 INTVARCHAR

进行比较

[infx1210@tardis ~]$ finderr 1213 -1213 A character to numeric conversion process failed.

A character value is being converted to numeric form for storage in a numeric column or variable. However, the character string cannot be interpreted as a number. It contains some characters other than white space, digits, a sign, a decimal, or the letter e; or the parts are in the wrong order, so the number cannot be deciphered.

If you are using NLS, the decimal character or thousands separator might be wrong for your locale.

[infx1210@tardis ~]$

然后您尝试将 VARCHAR 转换为 INT 导致了同样的错误,您应该尝试另一种方式:

> SELECT  t1.*
> FROM    tab1 AS t1
> WHERE   t1.col1::CHAR(11) IN (SELECT t2.col1 FROM tab2 AS t2);
>

       col1        col2

     169592           3

1 row(s) retrieved.

>

如果使用 EXISTS:

没有获得更快的结果,请同时检查
>   SELECT        t1.*
>   FROM    tab1 AS t1
>   WHERE   EXISTS (
>                       SELECT  1
>                       FROM    tab2 AS t2
>                       WHERE   t1.col1::CHAR(11) = t2.col1
>   );

       col1        col2

     169592           3

1 row(s) retrieved.

>

另一种可能的方法是只连接表:

> SELECT    t1.*
> FROM      tab1 AS t1
>               INNER JOIN tab2 AS t2
>                   ON (t1.col1 = t2.col1);

       col1        col2

     169592           3

1 row(s) retrieved.

>