SQL只获取字段中的中文字符

原文发布时间为:2010-10-28 —— 来源于本人的百度文章 [由搬家工具导入]

新建标量函数

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[fun_getCN](@str nvarchar(4000))  
returns nvarchar(4000)  
as  
begin  
declare @word nchar(1),@CN nvarchar(4000)  
set @CN=''  
while len(@str)>0  
begin  
set @word=left(@str,1)  
if unicode(@word) between 19968 and 19968+20901
set @CN=@CN+@word
set @str=right(@str,len(@str)-1)  
end  
return @CN  
end  

===

调用

select dbo.fun_getCN('ASDKG论坛KDL')
论坛
select dbo.fun_getCN('ASDKG論壇KDL')
論壇
select dbo.fun_getCN('ASDKDL')
空字符串

原文地址:https://www.cnblogs.com/handboy/p/7163960.html