字母转为小写字母

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

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

参考函数:

字母转为小写字母_第1张图片

 

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

 

例子:

字母转为小写字母_第2张图片

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另外写一个函数:

字母转为小写字母_第3张图片

 

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

 

实例演示:

字母转为小写字母_第4张图片

 

你可能感兴趣的:(字母转为小写字母)