--SQL判断字段值是否有中文
createfunctionfun_getCN(@strnvarchar(4000))
returnsnvarchar(4000)
as
begin
declare@wordnchar(1),@CNnvarchar(4000)
set@CN=''
whilelen(@str)>0
begin
set@word=left(@str,1)
ifunicode(@word)between19968and19968+20901
set@CN=@CN+@word
set@str=right(@str,len(@str)-1)
end
return@CN
end
selectdbo.fun_getCN('ASDKG论坛KDL')
--论坛
selectdbo.fun_getCN('ASDKG論壇KDL')
--論壇
selectdbo.fun_getCN('ASDKDL')
--空
-------------------------------------
--提取数字
IFOBJECT_ID('DBO.GET_NUMBER2')ISNOTNULL
DROPFUNCTIONDBO.GET_NUMBER2
GO
CREATEFUNCTIONDBO.GET_NUMBER2(@SVARCHAR(100))
RETURNSVARCHAR(100)
AS
BEGIN
WHILEPATINDEX('%[^0-9]%',@S)>0
BEGIN
set@s=stuff(@s,patindex('%[^0-9]%',@s),1,'')
END
RETURN@S
END
GO
--测试
PRINTDBO.GET_NUMBER('呵呵ABC123ABC')
GO
--123
--------------------------------------------------------------------
--提取英文
IFOBJECT_ID('DBO.GET_STR')ISNOTNULL
DROPFUNCTIONDBO.GET_STR
GO
CREATEFUNCTIONDBO.GET_STR(@SVARCHAR(100))
RETURNSVARCHAR(100)
AS
BEGIN
WHILEPATINDEX('%[^a-z]%',@S)>0
BEGIN
set@s=stuff(@s,patindex('%[^a-z]%',@s),1,'')
END
RETURN@S
END
GO
--测试
PRINTDBO.GET_STR('呵呵ABC123ABC')
GO
--------------------------------------------------------------------
--提取中文
IFOBJECT_ID('DBO.CHINA_STR')ISNOTNULL
DROPFUNCTIONDBO.CHINA_STR
GO
CREATEFUNCTIONDBO.CHINA_STR(@SNVARCHAR(100))
RETURNSVARCHAR(100)
AS
BEGIN
WHILEPATINDEX('%[^吖-座]%',@S)>0
SET@S=STUFF(@S,PATINDEX('%[^吖-座]%',@S),1,N'')
RETURN@S
END
GO
PRINTDBO.CHINA_STR('呵呵ABC123ABC')
GO
--------------------------------------------------------------------
--过滤重复字符
IFOBJECT_ID('DBO.DISTINCT_STR')ISNOTNULL
DROPFUNCTIONDBO.DISTINCT_STR
GO
CREATEFUNCTIONDBO.DISTINCT_STR(@SNVARCHAR(100),@SPLITVARCHAR(50))
RETURNSVARCHAR(100)
AS
BEGIN
IF@SISNULLRETURN(NULL)
DECLARE@NEWVARCHAR(50),@INDEXINT,@TEMPVARCHAR(50)
IFLEFT(@S,1)<>@SPLIT
SET@S=@SPLIT+@S
IFRIGHT(@S,1)<>@SPLIT
SET@S=@S+@SPLIT
WHILECHARINDEX(@SPLIT,@S)>0ANDLEN(@S)<>1
BEGIN
SET@INDEX=CHARINDEX(@SPLIT,@S)
SET@TEMP=LEFT(@S,CHARINDEX(@SPLIT,@S,@INDEX+LEN(@SPLIT)))
IF@NEWISNULL
SET@NEW=ISNULL(@NEW,'')+@TEMP
ELSE
SET@NEW=ISNULL(@NEW,'')+REPLACE(@TEMP,@SPLIT,'')+@SPLIT
WHILECHARINDEX(@TEMP,@S)>0
BEGIN
SET@S=STUFF(@S,CHARINDEX(@TEMP,@S)+LEN(@SPLIT),CHARINDEX(@SPLIT,@S,CHARINDEX(@TEMP,@S)+LEN(@SPLIT))-CHARINDEX(@TEMP,@S),'')
END
END
RETURNRIGHT(LEFT(@NEW,LEN(@NEW)-1),LEN(LEFT(@NEW,LEN(@NEW)-1))-1)
END
GO
PRINTDBO.DISTINCT_STR('A,A,B,C,C,B,C,',',')
--A,B,C
GO
--------------------------------------------------------------------
--过滤重复字符2
IFOBJECT_ID('DBO.DISTINCT_STR2')ISNOTNULL
DROPFUNCTIONDBO.DISTINCT_STR2
GO
CREATEFUNCTIONDBO.DISTINCT_STR2(@Svarchar(8000))
RETURNSVARCHAR(100)
AS
BEGIN
IF@SISNULLRETURN(NULL)
DECLARE@NEWVARCHAR(50),@INDEXINT,@TEMPVARCHAR(50)
WHILELEN(@S)>0
BEGIN
SET@NEW=ISNULL(@NEW,'')+LEFT(@S,1)
SET@S=REPLACE(@S,LEFT(@S,1),'')
END
RETURN@NEW
END
GO
SELECTDBO.DISTINCT_STR2('AABCCD')
--ABCD
GO
--------------------------------------------------------------------
IFOBJECT_ID('DBO.SPLIT_STR')ISNOTNULL
DROPFUNCTIONDBO.SPLIT_STR
GO
CREATEFUNCTIONDBO.SPLIT_STR(
@Svarchar(8000),--包含多个数据项的字符串
@INDEXint,--要获取的数据项的位置
@SPLITvarchar(10)--数据分隔符
)
RETURNSVARCHAR(100)
AS
BEGIN
IF@SISNULLRETURN(NULL)
DECLARE@SPLITLENint
SELECT@SPLITLEN=LEN(@SPLIT+'A')-2
WHILE@INDEX>1ANDCHARINDEX(@SPLIT,@S+@SPLIT)>0
SELECT@INDEX=@INDEX-1,@S=STUFF(@S,1,CHARINDEX(@SPLIT,@S+@SPLIT)+@SPLITLEN,'')
RETURN(ISNULL(LEFT(@S,CHARINDEX(@SPLIT,@S+@SPLIT)-1),''))
END
GO
PRINTDBO.SPLIT_STR('AA|BB|CC',2,'|')
--
GO