字母转为小写字母

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

如: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

 

实例演示:

 

posted @ 2019-05-31 19:45  Insus.NET  阅读(725)  评论(0编辑  收藏  举报