我们有一个名为House_Number的varchar2列,它包含所有不同类型的数据条目,如下所示。 我需要的是从左到第一次出现的非数字空格,特殊字符和ascii字符的数字。
以下是一些样本house_numbers:
217 3RDFL, 2173RDFL, 500D, 527 # 2ND, 527# 2ND, 5422NDFL, 5422, 30# D2,
1250 2ND, 12502ND, 217 3RDFL, 2173RDFL, 5241R, 3 2R, 32R, 5092R, 24 # 2R,
24# 2R, 129 B-16, 129# B17, 129B-16, 16 # 2B, 4229B, 539# APT 3, 563 # A5,
2162A, 934-A, 109-A, 511-A, 339-REA, 339REAR
对于上述house_numbers,预期的相应输出如下:
217, 217, 500, 527, 527, 542, 542, 30,1250,1250, 217, 217, 5241,
3, 32, 5092, 24, 24, 129, 129, 129, 16, 4229, 539, 563, 2162,
934, 109, 511, 339, 339
我尝试使用regexp_substr,但我无法获得适用于所有不同数据输入的逻辑,如上所示。
如果有人能提供帮助,我真的很感激。
答案 0 :(得分:1)
使用regexp_substr():
regexp_substr(house_number, '^[0-9]*')
匹配在单词([0-9]
)开头出现的任何数字(^
)重复任意次(*
)
答案 1 :(得分:0)
以下是我刚刚建立的一个可以调整/调整的解决方案: 的 SQL:强>
DECLARE @HOUSE_NUMBER_DATA TABLE
(
HOUSE_NUMBER VARCHAR(25)
)
INSERT INTO @HOUSE_NUMBER_DATA
SELECT '217 3RDFL ' UNION ALL
SELECT '2173RDFL ' UNION ALL
SELECT '500D ' UNION ALL
SELECT '527 # 2ND ' UNION ALL
SELECT '527# 2ND ' UNION ALL
SELECT '5422NDFL ' UNION ALL
SELECT '30# D2 ' UNION ALL
SELECT '1250 2ND ' UNION ALL
SELECT '12502ND ' UNION ALL
SELECT '217 3RDFL ' UNION ALL
SELECT '2173RDFL ' UNION ALL
SELECT '5241R ' UNION ALL
SELECT '3 2R ' UNION ALL
SELECT '32R ' UNION ALL
SELECT '5092R ' UNION ALL
SELECT '24 # 2R ' UNION ALL
SELECT '24# 2R ' UNION ALL
SELECT '129 B-16 ' UNION ALL
SELECT '129# B17 ' UNION ALL
SELECT '129B-16 ' UNION ALL
SELECT '16 # 2B ' UNION ALL
SELECT '4229B ' UNION ALL
SELECT '539# APT 3' UNION ALL
SELECT '563 # A5 ' UNION ALL
SELECT '2162A ' UNION ALL
SELECT '934-A ' UNION ALL
SELECT '109-A ' UNION ALL
SELECT '511-A ' UNION ALL
SELECT '339-REA ' UNION ALL
SELECT '339REAR '
SELECT * FROM @HOUSE_NUMBER_DATA
SELECT SUBSTRING(HOUSE_NUMBER,0, PATINDEX('%[^0-9]%', HOUSE_NUMBER)) ONLY_FIRST_NUMBERS_NO_SPACES
,CASE --It will tough to account for 22nd floor vs 2nd floor with no spaces
WHEN SUBSTRING(HOUSE_NUMBER, PATINDEX('%[^0-9]%',HOUSE_NUMBER),2) = 'RD' THEN SUBSTRING(HOUSE_NUMBER,0, PATINDEX('%[^0-9]%', HOUSE_NUMBER) -1)
WHEN SUBSTRING(HOUSE_NUMBER, PATINDEX('%[^0-9]%',HOUSE_NUMBER),2) = 'R' THEN SUBSTRING(HOUSE_NUMBER,0, PATINDEX('%[^0-9]%', HOUSE_NUMBER) -1)
WHEN SUBSTRING(HOUSE_NUMBER, PATINDEX('%[^0-9]%',HOUSE_NUMBER),2) = 'ND' THEN SUBSTRING(HOUSE_NUMBER,0, PATINDEX('%[^0-9]%', HOUSE_NUMBER) -1)
ELSE SUBSTRING(HOUSE_NUMBER,0, PATINDEX('%[^0-9]%', HOUSE_NUMBER))
END TRYING_TO_IGNORE_RD
,HOUSE_NUMBER
FROM @HOUSE_NUMBER_DATA
不知道如何将其转换为ORACLE。 (取出之前的ORACLE部分,因为代码似乎不正确,不能与ORACLE一起工作)