我有一个表,列的值来自一枚举。我需要建立一个TSQL功能把这些价值观以"友好的名字"后检索。

实例:

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

我需要一个直TSQL UDF的解决方案。我没有选择安装扩大存储程序或CLR的代码。

有帮助吗?

解决方案

/*
 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

其他提示

如果你使用SQL Server 2005中,你可以写一个本地CLR过程:

static string ToFriendlyCase(this string PascalString)
{
    return Regex.Replace(PascalString, "(?!^)([A-Z])", " $1");
}

输出:

  

转换的疯Pascal大小写句子友好案例

如果你的的使用2005年,那么你得手动解析它或使用扩展程序引用regex对象。一篇好文章可以在这里找到:

http://www.codeproject.com/KB/mcpp/xpregex.aspx

修改:一种UDF不能影响数据库,所以你不能注册正则表达式的COM对象,使蒙上这一想法出来。存储过程然而,可以 - 。因此,可能是一个路由

为了做一个区分大小写的比较,你将不得不设置排序的查询是区分大小写的,然后用一个取代我想......这里有可能是在指点你有帮助的文章断在正确的方向:

http://www.mssqltips.com/tip.asp?tip=1032

不是最好的解决方法,但它的工作原理:

declare @pascalCasedString nvarchar(max) = 'PascalCasedString'
declare @friendlyName nvarchar(max) = ''
declare @currentCode int;
declare @currentChar nvarchar;

while (LEN(@pascalCasedString) > 0)
    begin
        set @currentCode = UNICODE(@pascalCasedString)
        set @currentChar = NCHAR(@currentCode)

        if ((@currentCode >= 65) AND (@currentCode <= 90))
        begin
            set @friendlyName += SPACE(1)
        end
        set @friendlyName +=  @currentChar
        set @pascalCasedString = RIGHT(@pascalCasedString,LEN(@pascalCasedString) - 1)
    end

select @friendlyName

declare @arg varchar(20)
set @arg = 'DateOfBirthOnMonday'

declare @argLen int
set @argLen = len(@arg)

declare @output varchar(40)
set @output = ''

declare @i int
set @i = 1

declare @currentChar varchar(1)
declare @currentCharASCII int

while (1 = 1)
begin
set @currentChar = substring(@arg, @i, 1)
set @currentCharASCII = ascii(@currentChar)

if (@currentCharASCII >= 65 and @currentCharASCII <= 90)
set @output = @output + ' ' 

set @output = @output + @currentChar

set @i = @i+ 1

if (@i > @argLen) break
end

set @output = ltrim(rtrim(@output))
print @output

@arg的值更改为东西,你想测试。

此外,可能需要改变@output声明,以容纳具有相同的长度的它可能需要空格@arg +数字字符串。我已在例如一倍它。

我找到这工程完全一样,需要。礼貌的 SqlAuthority.com:

CREATE FUNCTION dbo.udf_TitleCase (@InputString VARCHAR(4000) )
RETURNS VARCHAR(4000)
AS
BEGIN
DECLARE @Index INT
DECLARE @Char CHAR(1)
DECLARE @OutputString VARCHAR(255)
SET @OutputString = LOWER(@InputString)
SET @Index = 2
SET @OutputString =
STUFF(@OutputString, 1, 1,UPPER(SUBSTRING(@InputString,1,1)))
WHILE @Index <= LEN(@InputString)
BEGIN
SET @Char = SUBSTRING(@InputString, @Index, 1)
IF @Char IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&','''','(')
IF @Index + 1 <= LEN(@InputString)
BEGIN
IF @Char != ''''
OR
UPPER(SUBSTRING(@InputString, @Index + 1, 1)) != 'S'
SET @OutputString =
STUFF(@OutputString, @Index + 1, 1,UPPER(SUBSTRING(@InputString, @Index + 1, 1)))
END
SET @Index = @Index + 1
END
RETURN ISNULL(@OutputString,'')
END

使用:

SELECT dbo.udf_TitleCase('This function will convert this string to title case!')

输出:

This Function Will Convert This String To Title Case!
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top