现在的位置: 首页 > 综合 > 正文

SQL Server 中查询非中文,非英文,非数字的特殊列

2013年04月01日 ⁄ 综合 ⁄ 共 1285字 ⁄ 字号 评论关闭

今天在处理一个用户名数据库时,发现有些不正常的数据存在,按照逻辑,用户名只能是数字,字母,下划线和纯中文这样的字符组合存在,不应该有其他组合存在,但是发现数据库中由于各种历史原因,有些不正常的存在,如何找到这些异常数据,在CSDN的 SQL Server 版问了这样两个问题,如下:

http://topic.csdn.net/u/20100111/14/529a21a1-3ea8-4263-a0d9-34e83be79b1d.html

http://topic.csdn.net/u/20100111/15/c2a124c5-bc5b-4626-86ce-c5b862cf5cff.html

感谢CSDN的网友帮忙解决了这个问题,下面就是解决方法的汇总:

if object_id('[t1]') is not null 
	drop table [t1]
create table [t1]([c] nvarchar(20))

insert [t1]
select 'aaa' union all		-- 此数据不应该被搜索到
select 'bcds' union all		-- 此数据不应该被搜索到
select 'a1' union all		-- 此数据不应该被搜索到
select '' union all		-- 此数据不应该被搜索到
select '^%' union all		-- 应该搜索到
select 'ew1' union all		-- 此数据不应该被搜索到
select '344' union all		-- 此数据不应该被搜索到
select '__' union all		-- 此数据不应该被搜索到
select '213_21' union all	-- 此数据不应该被搜索到	
select 'a_2' union all		-- 此数据不应该被搜索到
select 'd' union all		-- 此数据不应该被搜索到
select 'ddd' union all		-- 此数据不应该被搜索到
select '电风扇' union all	-- 此数据不应该被搜索到
select '★思寒★' union all	-- 应该搜索到
select 'Ω' union all	        -- 应该搜索到
select 'トントン' union all	-- 应该搜索到
select '***' union all	        -- 应该搜索到
select '///////' union all	-- 应该搜索到
select '@-@' union all	        -- 应该搜索到
select '@小慧' union all	        -- 应该搜索到
select '~*晓菊*~' union all	-- 应该搜索到
select '啊★洛' union all	-- 应该搜索到
select '不思議の夜' union all	-- 应该搜索到
select '(嘉宾)胡飞' union all	-- 应该搜索到
select '--------------'		-- 应该搜索到



select * from [t1] WHERE PATINDEX('%[0-9a-z_]%',c)=0 
and PATINDEX('%[^吖-座]%',c) <> 0

 

另外感慨一下,CSDN的 SQL Server 版 问出问题到解决问题,竟然比Google 搜索答案还要快, SQL Server 版 的牛人真多, 而且回答的这么快,实在让人吃惊。

抱歉!评论已关闭.