如何查找具有特殊字符的记录不匹配具有相同特殊字符的格式?

问题描述 投票:0回答:2

我正在查询在邮政编码列上使用SQL来检查列zipcodes中哪些记录与使用like子句的格式不匹配。

我尝试找到与格式匹配的记录数。然后我尝试了哪些zip记录中有一个连字符或' - '。伯爵不同。

我想找出哪些记录是连字符' - '并且与XXXXX-XXXX的格式不匹配的记录。

另外,我不认为'^'否定符号在这里起作用,因为它不在方括号'[]'中。试过,但没有奏效

我试过的查询:

从zipcode_table中选择count(*),其中zipcode_column如'% - %'

从zipcode_table中选择count(*),其中zipcode_column就像'_____-____'

sql postgresql zip postgis street-address
2个回答
1
投票

你似乎想要:

select count(*)
from zipcode_table
where zipcode_column like '%-%' and          -- has a hyphen
      zipcode_column not like '_____-____';  -- but not in the right place

您可能真的想检查其他位置的数字:

where zipcode_column like '%-%' and             -- has a hyphen
      zipcode_column !~ '^[0-9]{5}-[0-9]{4}$';  -- but not in the right 

1
投票
SELECT
    COUNT(*) AS all,
    COUNT(*) FILTER (WHERE zipcode LIKE '_____-____') AS correct_format,
    COUNT(*) FILTER (WHERE zipcode LIKE '%-%' AND zipcode NOT LIKE '_____-____') AS incorrect_format,
    COUNT(*) FILTER (WHERE zipcode NOT LIKE '%-%') AS no_hyphen
FROM zipcode
© www.soinside.com 2019 - 2024. All rights reserved.