用于查找仅包含特殊字符的行的 SQL 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2968042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:27:24  来源:igfitidea点击:

SQL query for finding rows with special characters only

sqlsql-server

提问by Lijo

I am working with SQL Server 2005.

我正在使用 SQL Server 2005。

I need to find out only those rows for which there is a special character in “Body” column. In the following scenario, the result should be only the row with TemplateID = 2. How do we write the query for this?

我只需要找出“正文”列中有特殊字符的那些行。在以下场景中,结果应该只是 TemplateID = 2 的行。我们如何为此编写查询?

CREATE TABLE #Template (TemplateID INT, Body VARCHAR(100))

INSERT INTO #Template (TemplateID,Body) VALUES (1,'abcd  1234')

INSERT INTO #Template (TemplateID,Body) VALUES (2,'#^!@')

Anything other than the following is a special character for this scenario

除以下内容外的任何内容都是此场景的特殊字符

1) Alphabtes

2) Digits

3) Space

回答by Tom H

SELECT
    TemplateID,
    Body
FROM
    #Template
WHERE
    Body LIKE '%[^0-9a-zA-Z ]%'

The stuff between the brackets says numbers (0-9), lowercase alphas (a-z), uppercase alphas (A-Z) and the space. The "^" makes that a "NOT" one of these things. Note that this is different though than NOT LIKE '%[0-9a-zA-Z ]%'

括号之间的内容表示数字 (0-9)、小写字母 (az)、大写字母 (AZ) 和空格。“^”使它成为这些东西中的一个“非”。请注意,这与 NOT LIKE '%[0-9a-zA-Z ]%' 不同