zoukankan      html  css  js  c++  java
  • 字母转为小写字母

    传入大写字母,输出为小字母,如果传入小写字母,直接输出小写字母。传入非字母,输出为‘’。

    如:A-->a;B-->b;C-->c;...Z-->z

    参考函数:

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author:      Insus.NET
    -- Blog:        https://insus.cnblogs.com
    -- Create date: 2019-05-31
    -- Update date: 2019-05-31
    -- Description: 字母转为小写字母A-->a;B-->b;C-->c;...Z-->z
    --              如果非字母转换为''
    -- =============================================
    CREATE FUNCTION [dbo].[svf_ConvertLetterToLowercaseLetter] 
    (
      @Letter CHAR(1)
    ) RETURNS CHAR(1)
    AS
    BEGIN
        DECLARE @LowercaseLetter CHAR(1) = ''          
        IF LEN(ISNULL(@Letter,'')) > 0 
        BEGIN
            IF ASCII(@Letter) % 65 + 1 <= 26
                SET @LowercaseLetter = CHAR(ASCII(@Letter) + (97 - 65)) 
            
            IF ASCII(@Letter) % 97 + 1 <= 26
                SET @LowercaseLetter = @Letter   
        END
        RETURN @LowercaseLetter
    END
    GO
    Source Code

    例子:

    SELECT
    [dbo].[svf_ConvertLetterToLowercaseLetter] ('A') AS 'A',
    [dbo].[svf_ConvertLetterToLowercaseLetter] ('a') AS 'a',
    [dbo].[svf_ConvertLetterToLowercaseLetter] ('B') AS 'B',
    [dbo].[svf_ConvertLetterToLowercaseLetter] ('b') AS 'b',
    [dbo].[svf_ConvertLetterToLowercaseLetter] ('C') AS 'C',
    [dbo].[svf_ConvertLetterToLowercaseLetter] ('c') AS 'c',
    [dbo].[svf_ConvertLetterToLowercaseLetter] ('#') AS '#'
    Source Code

    以上只是转换单独字母,如果转换为一串字符,字符中所有大写字母转为小写字母,那又该怎样操作呢? Insus.NET另外写一个函数:

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author:      Insus.NET
    -- Blog:        https://insus.cnblogs.com
    -- Create date: 2019-05-31
    -- Update date: 2019-05-31
    -- Description: 大写字符转为小写字母
    -- =============================================
    CREATE FUNCTION [dbo].[svf_ConvertToLowercase] 
    (
        @Letter NVARCHAR(MAX)
    ) 
    RETURNS NVARCHAR(MAX)
    AS
    BEGIN
        DECLARE @Lowercase NVARCHAR(MAX) = N'',@i INT = 1    
        WHILE @i <= LEN(@Letter) 
            BEGIN          
                DECLARE @currentChar CHAR(1) = SUBSTRING(@Letter, @i, 1)
                SET @Lowercase = @Lowercase + 
                CASE WHEN ASCII(@currentChar) % 65 + 1 <= 26 
                THEN CHAR(ASCII(@currentChar) + (97 - 65))    
                ELSE
                   @currentChar
                END 
       
                SET @I = @I + 1 
            END
            
        RETURN @Lowercase
    END
    GO
    Source Code

    实例演示:

  • 相关阅读:
    【C++和C#的区别杂谈】后自增运算符的结算时机
    个人作业——软件工程实践总结&个人技术博客
    Unity常见的三种数据本地持久化方案
    C++的逗号运算符
    米哈游--2020春招实习
    厦门飞鱼科技--2020春招实习
    tap4fun(成都尼必鲁)--2020春招实习
    腾讯IEG--2020春招实习
    吉比特&雷霆游戏--2020春招实习
    Docker 基础知识
  • 原文地址:https://www.cnblogs.com/insus/p/10957153.html
Copyright © 2011-2022 走看看