0

我正在通过 IBM 的 iNavigator 使用 SQL。我有以下查询:

SELECT
PWGEOADDR AS ADDRESS,
POSSTR(PWGEOADDR, ' ')-1 AS FIRSTWORDCHARS,
SUBSTR(PWGEOADDR, 1, POSSTR(PWGEOADDR, ' ')-1) AS HOUSECHAR,
INTEGER(SUBSTR(PWGEOADDR, 1, POSSTR(PWGEOADDR, ' ')-1)) AS HOUSENUMBER
FROM DSDLIB.PWPPERMITS

在我的结果窗格中,我看到如下结果:

ADDRESS                 FIRSTWORDCHARS   HOUSECHAR             HOUSENUMBER
----------------------------------------------------------------------------
1730 West 800 South    4                 1730                           1730
273 E Heather Road     3                 273                             273
56 N State Street      2                 56                               56
2080 S. Main           4                 2080                           2080
Across Oakcrest Dr.    6                 Across               ++++++++++++++
123 North Main         3                 123                             123

+ 字符是我在 SQL 窗口中看到的文字字符。我按原样复制并粘贴它们。

我的问题是:我无法查询在给定范围内具有 HOUSENUMBER 的地址记录,例如在 30 和 50 之间。我想忽略无法成功转换的行。每当我添加一个将 CAST 的结果与 ANYTHING 进行比较的 WHERE 语句时,我都会从 AS/400 收到以下错误:

SQL 状态:22023

供应商代码:-802

Message: [SQL0802] Data conversion or data mapping error. 
Cause . . . . . :   Error type 6 has occurred. 

错误类型及其含义有:

1 -- Arithmetic overflow. 

2 -- Floating point overflow. 

3 -- Floating point underflow. 

4 -- Floating point conversion error. 

5 -- Not an exact result. 

6 -- Numeric data that is not valid. 

7 -- Double-byte character set (DBCS) or UTF-8 data that is not valid. 

8 -- Division by zero. 

9 -- Hash value cannot be computed for the requested query. 

10 -- User-defined function returned a mapping error. 

11 -- Not valid length found in a varying-length column returned from an array result set. 

12 -- Result of a concatenation operation on a varying-length field exceeded the maximum allowed length of the result type. If the error occurred when assigning a value to a host variable of a FETCH, embedded SELECT, SET, or VALUES INTO statement, the host variable name is *N and the relative position of the host variable in the INTO clause is 0. If the host variable name is *N, the error occurred when attempting to resolve a search condition. If more than one data mapping error occurred, this is a description of the first error that occurred.  For a description of any other data mapping errors, see the previously listed messages in the job log. Recovery  . . . :   The error was caused by data that was not valid or that was too large.  Look at the previously listed messages in the job log (DSPJOBLOG command) or press F10 (Display messages in job log) on this display to determine what row and columns were involved in the error.  Correct the data and then try the request again.

我已经尝试过将 CAST 与 NULL、SNAN、INFINITY 和 ++++++++++++++ 进行比较。

4

1 回答 1

0

我认为以下将起作用。translate()这通过消除单词中的数字(使用)并检查字符串的长度来测试第一个单词是否为数字。然后将结果转换为整数:

SELECT PWGEOADDR AS ADDRESS, POSSTR(PWGEOADDR, ' ')-1 AS FIRSTWORDCHARS,
       SUBSTR(PWGEOADDR, 1, POSSTR(PWGEOADDR, ' ')-1) AS HOUSECHAR,
       (case when LENGTH(RTRIM(TRANSLATE(SUBSTR(PWGEOADDR, 1, POSSTR(PWGEOADDR, ' ')-1), '*', '0123456789'))) = 0
             then INTEGER(SUBSTR(PWGEOADDR, 1, POSSTR(PWGEOADDR, ' ')-1))
        end) AS HOUSENUMBER
FROM DSDLIB.PWPPERMITS;

要在where子句中使用,您可能希望将其设为子查询:

select t.*
from (SELECT PWGEOADDR AS ADDRESS, POSSTR(PWGEOADDR, ' ')-1 AS FIRSTWORDCHARS,
             SUBSTR(PWGEOADDR, 1, POSSTR(PWGEOADDR, ' ')-1) AS HOUSECHAR,
             (case when LENGTH(RTRIM(TRANSLATE(SUBSTR(PWGEOADDR, 1, POSSTR(PWGEOADDR, ' ')-1), '*', '0123456789'))) = 0
                   then INTEGER(SUBSTR(PWGEOADDR, 1, POSSTR(PWGEOADDR, ' ')-1))
              end) AS HOUSENUMBER
      FROM DSDLIB.PWPPERMITS
     ) t
where housenumber between 10 and 15;
于 2013-07-30T23:32:13.880 回答