【问题标题】:How to find out a record with special character not matching a type of format having the same special character?如何找出具有特殊字符的记录与具有相同特殊字符的格式类型不匹配?
【发布时间】:2019-05-01 14:41:01
【问题描述】:

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

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

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

另外,我认为'^' 否定符号在这里不起作用,因为它不在方括号'[]' 中。试过了,还是不行

我尝试过的查询:

select count(*) from zipcode_table where zipcode_column like '%-%'

select count(*) from zipcode_table where zipcode_column like '_____-____'

【问题讨论】:

    标签: sql postgresql zip postgis street-address


    【解决方案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 
    

    【讨论】:

    • 欣赏高质量的回复,戈登。 $符号是干什么用的?此外,还有一个“^”、一个“非”和一个按位非“~”。你能帮我理解它是如何工作的吗?
    • @VishwalShah 。 . .这些是正则表达式结构。好吧,~!~ 分别是正则表达式匹配和不匹配的 Postgres 构造。正则表达式本身中的^$用于将模式锚定在字符串的开头和结尾(即匹配整个字符串)。
    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 2020-12-25
      • 2011-04-11
      • 2012-09-07
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多