在文本字段中查找任何数字

时间:2018-10-18 20:47:58

标签: sql db2

我有一列包含纯文本数据。我正在寻找任何后跟整数'Sam 123',Sam 597“等的名称。

我尝试过

SELECT message, LENGTH(message)
FROM surveys
WHERE LENGTH(MESSAGE) = 6 

但是它返回字母数字数据。

2 个答案:

答案 0 :(得分:1)

从DB2 11.1开始,您可以使用正则表达式。您的查询应类似于:

template <typename Type> struct OneOf<Type> {
//                                   ^~~~~~
    Type value;
};

此正则表达式匹配:

  • 一个或多个字母,后跟
  • 单个空格,后跟
  • 一个或多个数字。

答案 1 :(得分:1)

对于所有DB2版本:

select message
from table(values 'Sam 123', 'Sam 597', 'Sam123', 'Sam', '123 Sam') t(message)
where xmlcast(xmlquery('fn:matches($s, "[a-zA-Z]+ [0-9]+")' passing t.message as "s") as int) = 1
相关问题