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

How to find out a record with special character not matching a type of format having the same special character?

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

我尝试查找符合格式的记录数。然后我尝试了哪些 zip 记录中有连字符或“-”。计数不同。

我想找出哪些记录是带有连字符“-”且不符合 XXXXX-XXXX 格式的记录。

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

我试过的查询:

select 来自 zipcode_table 的计数 (*) 其中 zipcode_column 喜欢 '%-%'

select 来自 zipcode_table 的计数 (*),其中 zipcode_column 类似于 '_____-____'

您似乎想要:

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 
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