当前位置: 移动技术网 > IT编程>数据库>MSSQL > 大写字母或小写字母转换为数字

大写字母或小写字母转换为数字

2019年06月01日  | 移动技术网IT编程  | 我要评论

电竞世界第二季,天机勿语txt,小飞b

大写字母或小写字母转换为数字a-->1,a-->1;b-->2,b-->2;c-->3,c-->3;...z-->26,z-->26
 如果非字母转换为-1


 

set ansi_nulls on
go
set quoted_identifier on
go

-- =============================================
-- author:      insus.net
-- blog:        https://insus.cnblogs.com
-- create date: 2019-05-23
-- update date: 2019-05-23
-- description: 大写字母或小写字母转换为数字a-->1,a-->1;b-->2,b-->2;c-->3,c-->3;...z-->26,z-->26
--                如果非字母转换为-1
-- =============================================
create function [dbo].[svf_convertlettertonumber] 
(
  @letter char(1)
) returns int
as
begin
    declare @ascii int = -1
    if len(isnull(@letter,'')) > 0 
    begin        
        if ascii(@letter) % 65 + 1 <= 26
            set @ascii = ascii(@letter) % 65+ 1

        if ascii(@letter) % 97 + 1 <=26
            set @ascii = ascii(@letter) % 97 + 1
    end        
    
    return @ascii
end
go

 

演示:

 

select 
[dbo].[svf_convertlettertonumber]('a') as [a],
[dbo].[svf_convertlettertonumber]('a') as [a],
[dbo].[svf_convertlettertonumber]('b') as [b],
[dbo].[svf_convertlettertonumber]('b') as [b],
[dbo].[svf_convertlettertonumber]('c') as [c],
[dbo].[svf_convertlettertonumber]('c') as [c],
[dbo].[svf_convertlettertonumber]('z') as [z],
[dbo].[svf_convertlettertonumber]('z') as [z],
[dbo].[svf_convertlettertonumber]('@') as [@],
[dbo].[svf_convertlettertonumber]('$') as [$]
go

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网