我正在通过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之间。我想忽略不能成功转换的行。每当我添加一个WHERE语句将强制转换的结果与任何内容进行比较时,我都会从AS/400中得到以下错误:
State: 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.我已经尝试过将强制转换与空、SNAN、无穷大和++++++++++++++进行比较。
发布于 2013-07-30 23:32:13
我想下面的方法会有效的。这个测试通过删除单词中的数字(使用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;https://stackoverflow.com/questions/17958971
复制相似问题