SQL Server 中查詢非中文,非英文,非數字的特殊列

今天在處理一個用戶名數據庫時,發現有些不正常的數據存在,按照邏輯,用戶名只能是數字,字母,下劃線和純中文這樣的字符組合存在,不應該有其他組合存在,但是發現數據庫中由於各種歷史原因,有些不正常的存在,如何找到這些異常數據,在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 版 的牛人真多, 而且回答的這麼快,實在讓人吃驚。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章