3

値が列挙型から取得される列を持つテーブルがあります。これらの値を取得時に「わかりやすい名前」に変換する TSQL 関数を作成する必要があります。

例:

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

単純な TSQL UDF ソリューションが必要です。拡張ストア プロシージャまたは CLR コードをインストールするオプションがありません。

4

5 に答える 5

3
/*
 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
于 2009-05-07T23:16:44.573 に答える
1

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の値を、テストしたいものに変更します。

また、@ arg +必要なスペースの数と同じ長さの文字列に対応するために、@output宣言を変更する必要がある場合があります。私の例ではそれを2倍にしました。

于 2009-05-07T23:22:27.367 に答える
1

最も洗練されたソリューションではありませんが、機能します。

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
于 2009-05-07T23:28:05.420 に答える
1

SQL Server 2005 を使用している場合は、ネイティブ CLR プロシージャを記述できます。

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

出力:

My Crazy Pascal Case Sentence を Friendly Case に変換する

2005 を使用していない場合は、手動で解析するか、拡張プロシージャを使用して正規表現オブジェクトを参照する必要があります。良い記事はここにあります:

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

編集: UDF はデータベースに影響を与えることができないため、正規表現 com オブジェクトを登録することはできません。ただし、ストアド プロシージャはできるので、それがルートになる可能性があります。

大文字と小文字を区別して比較するには、クエリの照合順序で大文字と小文字を区別するように設定してから、置換を使用する必要があります。右方向:

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

于 2009-05-07T22:43:33.543 に答える
0

これは必要に応じて正確に機能することがわかりました。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!
于 2013-12-03T11:03:41.883 に答える