SQL从文本列获取数字行

时间:2012-08-16 18:41:25

标签: sql db2

如何在DB2 SQL查询中查看文本字段是否为非数字?

我在表格中有一个文本字段列,我只想查询100%数字的行。

4 个答案:

答案 0 :(得分:1)

假设该字段长度为10位(与我的一样),这将是解决方案。

WHERE
  (  
     SUBSTRING(ID_NUM,1,1,OCTETS) BETWEEN '0' AND '9'
  OR SUBSTRING(ID_NUM,2,1,OCTETS) BETWEEN '0' AND '9'
  OR SUBSTRING(ID_NUM,3,1,OCTETS) BETWEEN '0' AND '9'
  OR SUBSTRING(ID_NUM,4,1,OCTETS) BETWEEN '0' AND '9'
  OR SUBSTRING(ID_NUM,5,1,OCTETS) BETWEEN '0' AND '9'
  OR SUBSTRING(ID_NUM,6,1,OCTETS) BETWEEN '0' AND '9'
  OR SUBSTRING(ID_NUM,7,1,OCTETS) BETWEEN '0' AND '9'
  OR SUBSTRING(ID_NUM,8,1,OCTETS) BETWEEN '0' AND '9'
  OR SUBSTRING(ID_NUM,9,1,OCTETS) BETWEEN '0' AND '9'
  OR SUBSTRING(ID_NUM,10,1,OCTETS)BETWEEN '0' AND '9'
  )

答案 1 :(得分:0)

智能解决方案来自:http://db2portal.blogspot.com.es/2006/03/returning-only-numeric-data.html

SELECT *
FROM TABLENAME
WHERE UCASE(VAR) = LCASE(VAR);

(另外,您应检查不包含-.(等特殊字符,...)

另一个:

SELECT *
FROM TABLENAME
WHERE TRANSLATE(VAR4,'##########','1234567890') = '####'
;

对我而言最好:

SELECT *
FROM Table
WHERE TRANSLATE(Field,
' ',
'0123456789,. '
) = ''
AND Field <> ''

答案 2 :(得分:0)

我发现此查询更适合识别非数字列。

select  a.* from table a where (substring(testcol,1,1) 
not in ('1','2','3','4','5','6','7','8','9','0') 
or substring(testcol,2,1) not  in ('1','2','3','4','5','6','7','8','9','0') 
or substring(testcol ,3,1) not in ('1','2','3','4','5','6','7','8','9','0') 
or substring(testcol,4,1) not in ('1','2','3','4','5','6','7','8','9','0') 
or substring(testcol,5,1) not in ('1','2','3','4','5','6','7','8','9','0') 
or substring(testcol,6,1) not in ('1','2','3','4','5','6','7','8','9','0') 
or substring(testcol,7,1) not in ('1','2','3','4','5','6','7','8','9','0') 
or substring(testcol ,8,1) not in ('1','2','3','4','5','6','7','8','9','0')) 

答案 3 :(得分:-1)

从mytable中选择myfield,其中isnumeric(myfield)