将 PascalCase 字符串转换为“友好名称"在 TSQL 中 [英] Converting PascalCase string to "Friendly Name" in TSQL

查看:31
本文介绍了将 PascalCase 字符串转换为“友好名称"在 TSQL 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中一列的值来自枚举.我需要创建一个 TSQL 函数,以便在检索时将这些值转换为友好名称".

I have a table with a column whose values come from an Enumeration. I need to create a TSQL function to convert these values to "Friendly Names" upon retrieval.

示例:

 'DateOfBirth' --> 'Date Of Birth'
 'PrincipalStreetAddress' --> 'Principal Street Address'

我需要一个直接的 TSQL UDF 解决方案.我没有安装扩展存储过程或 CLR 代码的选项.

I need a straight TSQL UDF solution. I don't have the option of installing Extended Store Procedures or CLR code.

推荐答案

/*
 Try this.  It's a first hack - still has problem of adding extra space
 at start if first char is in upper case.
*/
create function udf_FriendlyName(@PascalName varchar(max))
returns varchar(max)
as
begin

    declare @char char(1)
    set @char = 'A'

    -- Loop through the letters A - Z, replace them with a space and the letter
    while ascii(@char) <= ascii('Z')
    begin
        set @PascalName = replace(@PascalName, @char collate Latin1_General_CS_AS, ' ' + @char) 
        set @char = char(ascii(@char) + 1)
    end

    return LTRIM(@PascalName) --remove extra space at the beginning

end

这篇关于将 PascalCase 字符串转换为“友好名称"在 TSQL 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆