简体   繁体   English

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

[英]SQL query for finding rows with special characters only

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. 我只需找出“Body”列中有特殊字符的那些行。 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
SELECT
    TemplateID,
    Body
FROM
    #Template
WHERE
    Body LIKE '%[^0-9a-zA-Z ]%'

The stuff between the brackets says numbers (0-9), lowercase alphas (az), uppercase alphas (AZ) and the space. 括号之间的东西表示数字(0-9),小写字母alpha(az),大写字母alpha(AZ)和空格。 The "^" makes that a "NOT" one of these things. “^”使其成为“NOT”中的一个。 Note that this is different though than NOT LIKE '%[0-9a-zA-Z ]%' 请注意,这与NOT LIKE'%[0-9a-zA-Z]%'不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM