在IBM SQL中选择范围内的地址记录

时间:2013-07-30 23:03:33

标签: sql parsing casting iseries-navigator

我正在通过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的ADDRESS记录,例如BETWEEN 30 AND 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和++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1 个答案:

答案 0 :(得分: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;