SQL Server 2008 查询查找列中包含非字母数字字符的行

发布于 2024-08-14 21:00:25 字数 463 浏览 4 评论 0原文

事实上,几周前我自己也被问过这个问题,虽然我确切地知道如何使用 SP 或 UDF 来做到这一点,但我想知道是否有一种快速而简单的方法可以在没有这些方法的情况下做到这一点。我假设有,但我找不到它。

我需要指出的一点是,尽管我们知道允许使用哪些字符(az、AZ、0-9),但我们不想指定不允许使用哪些字符(#@!$ 等)。 .)。另外,我们希望提取非法字符的行,以便可以将其列出给用户进行修复(因为我们无法控制输入过程,所以此时我们无法执行任何操作) )。

我之前浏览过 SO 和 Google,但找不到任何可以满足我要求的东西。我见过很多例子,它们可以告诉你它是否包含字母数字字符,或者不包含字母数字字符,但是我在查询形式中没有找到能够在句子中拉出撇号的东西。

另请注意,此 varchar 列中的值可以为 null''(空)。

I was actually asked this myself a few weeks ago, whereas I know exactly how to do this with a SP or UDF but I was wondering if there was a quick and easy way of doing this without these methods. I'm assuming that there is and I just can't find it.

A point I need to make is that although we know what characters are allowed (a-z, A-Z, 0-9) we don't want to specify what is not allowed (#@!$ etc...). Also, we want to pull the rows which have the illegal characters so that it can be listed to the user to fix (as we have no control over the input process we can't do anything at that point).

I have looked through SO and Google previously, but was unable to find anything that did what I wanted. I have seen many examples which can tell you if it contains alphanumeric characters, or doesn't, but something that is able to pull out an apostrophe in a sentence I have not found in query form.

Please note also that values can be null or '' (empty) in this varchar column.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甩你一脸翔 2024-08-21 21:00:25

这还不行吗?

SELECT * FROM TABLE
WHERE COLUMN_NAME LIKE '%[^a-zA-Z0-9]%'

设置

use tempdb
create table mytable ( mycol varchar(40) NULL)

insert into mytable VALUES ('abcd')
insert into mytable VALUES ('ABCD')
insert into mytable VALUES ('1234')
insert into mytable VALUES ('efg%^&hji')
insert into mytable VALUES (NULL)
insert into mytable VALUES ('')
insert into mytable VALUES ('apostrophe '' in a sentence') 

SELECT * FROM mytable
WHERE mycol LIKE '%[^a-zA-Z0-9]%'

drop table mytable 

结果

mycol
----------------------------------------
efg%^&hji
apostrophe ' in a sentence

Won't this do it?

SELECT * FROM TABLE
WHERE COLUMN_NAME LIKE '%[^a-zA-Z0-9]%'

Setup

use tempdb
create table mytable ( mycol varchar(40) NULL)

insert into mytable VALUES ('abcd')
insert into mytable VALUES ('ABCD')
insert into mytable VALUES ('1234')
insert into mytable VALUES ('efg%^&hji')
insert into mytable VALUES (NULL)
insert into mytable VALUES ('')
insert into mytable VALUES ('apostrophe '' in a sentence') 

SELECT * FROM mytable
WHERE mycol LIKE '%[^a-zA-Z0-9]%'

drop table mytable 

Results

mycol
----------------------------------------
efg%^&hji
apostrophe ' in a sentence
待天淡蓝洁白时 2024-08-21 21:00:25

Sql 服务器的正则表达式支持非常有限。您可以将 PATINDEX 与类似的内容一起使用

PATINDEX('%[a-zA-Z0-9]%',Col)

看看 PATINDEX (Transact-SQL) < /a>

搜索条件中的模式匹配

Sql server has very limited Regex support. You can use PATINDEX with something like this

PATINDEX('%[a-zA-Z0-9]%',Col)

Have a look at PATINDEX (Transact-SQL)

and Pattern Matching in Search Conditions

飘然心甜 2024-08-21 21:00:25

我发现 这个页面有一个相当简洁的解决方案。它的伟大之处在于你可以了解角色是什么以及它在哪里。然后它提供了一种超级简单的方法来修复它(可以将其组合并构建到一段驱动程序代码中以扩展其应用程序)。

DECLARE @tablename VARCHAR(1000) ='Schema.Table'
DECLARE @columnname VARCHAR(100)='ColumnName'
DECLARE @counter INT = 0
DECLARE @sql VARCHAR(MAX)

WHILE @counter <=255
BEGIN

SET @sql=

'SELECT TOP 10 '+@columnname+','+CAST(@counter AS VARCHAR(3))+' as CharacterSet, CHARINDEX(CHAR('+CAST(@counter AS VARCHAR(3))+'),'+@columnname+') as LocationOfChar
FROM '+@tablename+'
WHERE CHARINDEX(CHAR('+CAST(@counter AS VARCHAR(3))+'),'+@columnname+') <> 0'

PRINT (@sql)
EXEC (@sql)
SET @counter = @counter + 1
END

然后……

UPDATE Schema.Table
SET ColumnName= REPLACE(Columnname,CHAR(13),'')

感谢艾曼·安萨里。

I found this page with quite a neat solution. What makes it great is that you get an indication of what the character is and where it is. Then it gives a super simple way to fix it (which can be combined and built into a piece of driver code to scale up it's application).

DECLARE @tablename VARCHAR(1000) ='Schema.Table'
DECLARE @columnname VARCHAR(100)='ColumnName'
DECLARE @counter INT = 0
DECLARE @sql VARCHAR(MAX)

WHILE @counter <=255
BEGIN

SET @sql=

'SELECT TOP 10 '+@columnname+','+CAST(@counter AS VARCHAR(3))+' as CharacterSet, CHARINDEX(CHAR('+CAST(@counter AS VARCHAR(3))+'),'+@columnname+') as LocationOfChar
FROM '+@tablename+'
WHERE CHARINDEX(CHAR('+CAST(@counter AS VARCHAR(3))+'),'+@columnname+') <> 0'

PRINT (@sql)
EXEC (@sql)
SET @counter = @counter + 1
END

and then...

UPDATE Schema.Table
SET ColumnName= REPLACE(Columnname,CHAR(13),'')

Credit to Ayman El-Ghazali.

落花随流水 2024-08-21 21:00:25
SELECT * FROM TABLE_NAME WHERE COL_NAME LIKE '%[^0-9a-zA-Z $@$.$-

当我试图在字符串中查找任何特殊字符时,这对我来说最有效

'''$,]%'

当我试图在字符串中查找任何特殊字符时,这对我来说最有效

SELECT * FROM TABLE_NAME WHERE COL_NAME LIKE '%[^0-9a-zA-Z $@$.$-

This works best for me when I'm trying to find any special characters in a string

'''$,]%'

This works best for me when I'm trying to find any special characters in a string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文