[Sql Server][原创] - 返回指定范围的Unicode字符 And 返回字符串的字节数(汉字二个,字母一个)
-- 返回指定范围的Unicode字符
USE [EPICOR10] GO /****** Object: UserDefinedFunction [dbo].[AH_NCharTable] Script Date: 2017-12-21 20:44:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO /* 函数: Erp.AH_NCharTable(@Begin,@End)
作者 :929412592@qq.com 辉创1989 作用: 返回指定范围的Unicode字符 测试: -- select * from Dbo.AH_NCharTable(0,47) where Len(NCharStr) > 0 Order By NChar10 -- 数字0-9 -- 10 select * from Dbo.AH_NCharTable(48,57) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0X00000030,0X00000039) where Len(NCharStr) > 0 Order By NChar10 -- select * from Dbo.AH_NCharTable(58,64) where Len(NCharStr) > 0 Order By NChar10 -- 大写字母A-Z -- 26 select * from Dbo.AH_NCharTable(65,90) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0X00000041,0X0000005A) where Len(NCharStr) > 0 Order By NChar10 -- select * from Dbo.AH_NCharTable(91,96) where Len(NCharStr) > 0 Order By NChar10 -- 小写字母a-z -- 26 select * from Dbo.AH_NCharTable(97,122) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0X00000061,0X0000007A) where Len(NCharStr) > 0 Order By NChar10 -- select * from Dbo.AH_NCharTable(123,4351) where Len(NCharStr) > 0 Order By NChar10 -- select * from Dbo.AH_NCharTable(4352,4601) where Len(NCharStr) > 0 Order By NChar10 -- select * from Dbo.AH_NCharTable(4602,8543) where Len(NCharStr) > 0 Order By NChar10 -- select * from Dbo.AH_NCharTable(8544,8575) where Len(NCharStr) > 0 Order By NChar10 -- select * from Dbo.AH_NCharTable(8576,8591) where Len(NCharStr) > 0 Order By NChar10 -- select * from Dbo.AH_NCharTable(8592,9311) where Len(NCharStr) > 0 Order By NChar10 -- select * from Dbo.AH_NCharTable(9312,9470) where Len(NCharStr) > 0 Order By NChar10 -- select * from Dbo.AH_NCharTable(9471,11903) where Len(NCharStr) > 0 Order By NChar10 -- 部首扩展 -- 116字 select * from Dbo.AH_NCharTable(11904,12019) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x00002E80,0x00002EF3) where Len(NCharStr) > 0 Order By NChar10 -- -- select * from Dbo.AH_NCharTable(12020,12031) where Len(NCharStr) > 0 Order By NChar10 -- 康熙部首 -- 214字 select * from Dbo.AH_NCharTable(12032,12245) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x00002F00,0x00002FD5) where Len(NCharStr) > 0 Order By NChar10 -- -- select * from Dbo.AH_NCharTable(12246,12271) where Len(NCharStr) > 0 Order By NChar10 -- 汉字结构 -- 12字 select * from Dbo.AH_NCharTable(12272,12283) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x00002FF0,0x00002FFB) where Len(NCharStr) > 0 Order By NChar10 -- -- select * from Dbo.AH_NCharTable(12284,12294) where Len(NCharStr) > 0 Order By NChar10 -- 〇 -- 1 select * from Dbo.AH_NCharTable(12295,12295) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x00003007,0x00003007) where Len(NCharStr) > 0 Order By NChar10 -- -- select * from Dbo.AH_NCharTable(12296,12548) where Len(NCharStr) > 0 Order By NChar10 -- 汉语注音 -- 28字 select * from Dbo.AH_NCharTable(12549,12576) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x00003105,0x00003120) where Len(NCharStr) > 0 Order By NChar10 -- -- select * from Dbo.AH_NCharTable(12577,12703) where Len(NCharStr) > 0 Order By NChar10 -- 注音扩展 -- 27字 select * from Dbo.AH_NCharTable(12704,12730) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0X000031A0,0X000031BA) where Len(NCharStr) > 0 Order By NChar10 -- -- select * from Dbo.AH_NCharTable(12731,12735) where Len(NCharStr) > 0 Order By NChar10 -- 汉字笔画 -- 36字 select * from Dbo.AH_NCharTable(12736,12771) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x000031C0,0x000031E3) where Len(NCharStr) > 0 Order By NChar10 -- -- 403 select * from Dbo.AH_NCharTable(12772,13174) where Len(NCharStr) > 0 Order By NChar10 -- -- 4 select * from Dbo.AH_NCharTable(13175,13178) where Len(NCharStr) > 0 Order By NChar10 -- -- 132 select * from Dbo.AH_NCharTable(13179,13310) where Len(NCharStr) > 0 Order By NChar10 -- -- 1 select * from Dbo.AH_NCharTable(13311,13311) where Len(NCharStr) > 0 Order By NChar10 -- 汉字扩展A -- 6582 select * from Dbo.AH_NCharTable(13312,19893) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x00003400,0x00004DB5) where Len(NCharStr) > 0 Order By NChar10 -- -- 74 select * from Dbo.AH_NCharTable(19894,19967) where Len(NCharStr) > 0 Order By NChar10 -- 基本汉字 -- 20902 select * from Dbo.AH_NCharTable(19968,40869) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x00004E00,0x00009FA5) where Len(NCharStr) > 0 Order By NChar10 -- 基本汉字补充 -- 38 select * from Dbo.AH_NCharTable(40870,40907) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x00009FA6,0x00009FCB) where Len(NCharStr) > 0 Order By NChar10 -- -- select * from Dbo.AH_NCharTable(40908,58367) where Len(NCharStr) > 0 Order By NChar10 -- 部件扩展 -- 452字 select * from Dbo.AH_NCharTable(58368,58856) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x0000E400,0x0000E5E8) where Len(NCharStr) > 0 Order By NChar10 -- -- select * from Dbo.AH_NCharTable(58857,58879) where Len(NCharStr) > 0 Order By NChar10 -- PUA增补 -- 208 select * from Dbo.AH_NCharTable(58880,59087) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x0000E600,0x0000E6CF) where Len(NCharStr) > 0 Order By NChar10 -- PUA(GBK)部件 -- 91字 select * from Dbo.AH_NCharTable(59413,59503) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x0000E815,0x0000E86F) where Len(NCharStr) > 0 Order By NChar10 -- 兼容汉字 474字 select * from Dbo.AH_NCharTable(63744,64217) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x0000F900,0x0000FAD9) where Len(NCharStr) > 0 Order By NChar10 -- -- select * from Dbo.AH_NCharTable(64218,65535) where Len(NCharStr) > 0 Order By NChar10 -- -- select * from Dbo.AH_NCharTable(65536,130666) where Len(NCharStr) > 0 Order By NChar10 -- 扩展B -- select * from Dbo.AH_NCharTable(131072,173782) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x00020000,0x0002A6D6) where Len(NCharStr) > 0 Order By NChar10 -- 扩展C -- select * from Dbo.AH_NCharTable(173824,177972) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x0002A700,0x0002B734) where Len(NCharStr) > 0 Order By NChar10 -- 扩展D -- select * from Dbo.AH_NCharTable(177984,178205) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x0002B740,0x0002B81D) where Len(NCharStr) > 0 Order By NChar10 -- 兼容扩展 542字 select * from Dbo.AH_NCharTable(194560,195101) where Len(NCharStr) > 0 Order By NChar10 select * from Dbo.AH_NCharTable(0x0002F800,0x0002FA1D) where Len(NCharStr) > 0 Order By NChar10 select Convert(VarBinary,19968) select Convert(int,0x00004DB1) select sys.fn_varbintohexstr(19968) select sys.fn_varbintohexstr(0x00004E00) select NChar(19967),NChar(19968),NChar(40869),NChar(40870),NChar(0) */ ALTER Function [dbo].[AH_NCharTable] ( @Begin int, @End int ) Returns @NCharTable Table ( NCharSeq int identity(1,1) primary key, NChar10 int, NChar16 VarBinary(max), NCharStr nchar, CharStr char, NCharStrLen int, NCharStrExamples nvarchar(max), NCharStrExamplesLen int, NCharRange10 nvarchar(max), NCharRange16 nvarchar(max) ) As begin declare @NCharInd int = @Begin while(@NCharInd >= @Begin and @NCharInd <= @End) begin insert into @NCharTable(NChar10,NChar16,NCharStr,CharStr,NCharStrLen,NCharStrExamples) select @NCharInd as NChar10, Convert(VarBinary,@NCharInd) as NChar16, NChar(@NCharInd) as NCharStr, Char(@NCharInd) as CharStr, Len(NChar(@NCharInd)) as NCharStrLen, N'我举个栗子[' + NChar(@NCharInd) + N']栗子' as NCharStrExamples set @NCharInd += 1 end update @NCharTable set NCharStrExamplesLen = Len(NCharStrExamples) update @NCharTable set NCharRange10 = (select Convert(nvarchar(max),Min(NChar10)) from @NCharTable) + ',' + (select Convert(nvarchar(max),Max(NChar10)) from @NCharTable) update @NCharTable set NCharRange16 = (select Upper(Min(sys.fn_varbintohexstr(NChar16))) from @NCharTable) + ',' + (select Upper(Max(sys.fn_varbintohexstr(NChar16))) from @NCharTable) return end
-- 返回字符串的字节数(汉字二个,字母一个)
USE [EPICOR10] GO /****** Object: UserDefinedFunction [Erp].[AH_GetStrLen] Script Date: 2017-12-22 00:28:18 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO /* 函数: Dbo.AH_GetStrLen(@Str) 作者 :929412592@qq.com 辉创1989 作用: 返回字符串的字节数(汉字二个,字母一个) 参数: @Str 测试: select Dbo.AH_GetStrLen(N'外部MES') as StrLen select Dbo.AH_GetStrLen(N'號MES') as StrLen declare @Str nvarchar(max) = N'外部MES' select SubString(@Str,1,1),Replace(@Str,SubString(@Str,1,1),'') select NChar(19967),NChar(19968),NChar(40869),NChar(40870) select * from Dbo.AH_NCharTable(19968,40869) where Len(NCharStr) > 0 Order By NChar10 -- 0X00004E00,0X00009FA5 -- 20902 -- 基本汉字 select Unicode(N'丁') */ Alter function [Dbo].[AH_GetStrLen] ( @Str nvarchar(max) ) returns int as begin declare @Return int = 0, @Chr nchar, @StrCopy nvarchar(max) set @StrCopy = @Str while(Len(@StrCopy) > 0) begin set @Chr = SubString(@StrCopy,1,1) set @Return += Case When Unicode(@Chr) >= 19968 and Unicode(@Chr) <= 40869 Then 2 Else 1 End set @StrCopy = Replace(@StrCopy,SubString(@StrCopy,1,1),'') end return(@Return) end
博客标明【原创】的文章都是本人亲自编写内容!
如有需要转载,
请标明出处:辉创1989(http://www.cnblogs.com/ahui1989/),届时非常感谢!
文章分享在此,希望我之原创有帮到你们!
如有不足之处也可联系我,以便我们共同探讨!
本人现职为Epicor10 系统 开发维护工作,如有需要可共同探讨相关技术知识及经验总结!
QQ:929412592