代码之家  ›  专栏  ›  技术社区  ›  Andy Johnson

用replace()选择

  •  28
  • Andy Johnson  · 技术社区  · 16 年前

    我有一张姓名和地址表,其中有一个邮政编码栏。我想从邮政编码中去掉空格,并选择任何与特定模式匹配的空格。我在SQL Server 2005上的T-SQL中尝试了这个(简化了一点):

    SELECT Replace(Postcode, ' ', '') AS P
    FROM Contacts
    WHERE P LIKE 'NW101%'
    

    但我犯了以下错误;

    Msg 207, Level 16, State 1, Line 3
    Invalid column name 'P'.
    

    如果我删除where子句,就会得到一个没有空格的邮政编码列表,这就是我要搜索的内容。我该如何处理?我做错什么了?

    8 回复  |  直到 9 年前
        1
  •  37
  •   Oded    16 年前

    不要使用别名( P 在你 WHERE 直接从句。

    你也可以用同样的 REPLACE 逻辑在 哪里 条款:

    SELECT Replace(Postcode, ' ', '') AS P
    FROM Contacts
    WHERE Replace(Postcode, ' ', '') LIKE 'NW101%'
    

    或者使用nick的答案中描述的别名子查询。

        2
  •  11
  •   Nick Craver    16 年前

    如果您包装查询,可以这样引用:

    SELECT P
    FROM (SELECT Replace(Postcode, ' ', '') AS P
          FROM Contacts) innertable
    WHERE P LIKE 'NW101%'
    

    请确保为包装的select提供一个别名,即使是未使用的别名(如果没有iirc,sql server也不允许这样做)

        3
  •  3
  •   Sarfraz    16 年前

    您正在创建别名 P 后来在 where 子句您使用的是相同的,这就是造成问题的原因。不要使用 在里面 哪里 ,请改为:

    SELECT Replace(Postcode, ' ', '') AS P FROM Contacts
    WHERE Postcode LIKE 'NW101%'
    
        4
  •  3
  •   Community Mohan Dere    9 年前

    展开 Oded's answer ,您的概念模型需要在此处稍作调整。列名的别名( AS 中的子句 SELECT 列表)在处理 选择 ,这就是为什么别名不可用于 WHERE 条款。实际上,列别名之后发生的唯一事情是排序,这就是为什么(引用 选择 ):

    column_alias 可以在ORDER BY子句中使用。但是,它不能用于 哪里 , GROUP BY HAVING 条款。

    如果在 选择 当它出现在 选择 列出并(说)a 哪里 子句-但是,查询引擎足够聪明,可以计算出发生了什么。如果要避免表达式在查询中出现两次,可以执行以下操作

    SELECT c1, c2, c3, expr1
    FROM
        ( SELECT c1, c2, c3, some_complicated_expression AS expr1 ) inner
    WHERE expr1 = condition
    

    避免 some_complicated_expression 身体上出现两次。

        5
  •  3
  •   KM.    16 年前

    如果您希望使用索引,请以一致的方式(删除空格)存储数据。只需删除空格或添加持久化计算列,然后就可以从该列中进行选择,而不必每次运行查询时都添加所有的空间删除开销。

    添加持久化计算列:

    ALTER TABLE Contacts ADD PostcodeSpaceFree AS Replace(Postcode, ' ', '') PERSISTED 
    go
    CREATE NONCLUSTERED INDEX IX_Contacts_PostcodeSpaceFree 
    ON Contacts (PostcodeSpaceFree) --INCLUDE (covered columns here!!)
    go
    

    要通过删除空格来修复列,请使用:

    UPDATE Contacts
        SET Postcode=Replace(Postcode, ' ', '')
    

    现在您可以这样搜索,选择可以使用索引:

    --search the PERSISTED computed column
    SELECT 
        PostcodeSpaceFree 
        FROM Contacts
        WHERE PostcodeSpaceFree  LIKE 'NW101%'
    

    --search the fixed (spaces removed column)
    SELECT 
        Postcode
        FROM Contacts
        WHERE PostcodeLIKE 'NW101%'
    
        6
  •  2
  •   Damien_The_Unbeliever    16 年前

    你必须在任何你想用的地方重复你的表达:

    SELECT Replace(Postcode, ' ', '') AS P
    FROM Contacts
    WHERE Replace(Postcode, ' ', '') LIKE 'NW101%'
    

    也可以将其设为子查询

    select P
    from (
    SELECT Replace(Postcode, ' ', '') AS P
    FROM Contacts
    ) t
    WHERE P LIKE 'NW101%'
    
        7
  •  2
  •   Anthony Faull    16 年前
    SELECT *
    FROM Contacts
    WHERE ContactId IN
        (SELECT a.ContactID
        FROM
            (SELECT ContactId, Replace(Postcode, ' ', '') AS P
            FROM Contacts
            WHERE Postcode LIKE '%N%W%1%0%1%') a
        WHERE a.P LIKE 'NW101%')
    
        8
  •  0
  •   Peter B    9 年前

    这将起作用:

    SELECT Replace(Postcode, ' ', '') AS P
    FROM Contacts
    WHERE Replace(Postcode, ' ', '') LIKE 'NW101%'
    
    推荐文章