문제

나는 열거에서 나온 열이있는 테이블이 있습니다. 검색시 이러한 값을 "친절한 이름"으로 변환하려면 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");
}

출력 :

내 미친 파스칼 케이스 문장을 친절한 케이스로 변환하십시오

당신이 ~ 아니다 2005 년을 사용하면 수동으로 구문 분석하거나 확장 프로 시저를 사용하여 Regex 객체를 참조해야합니다. 좋은 기사는 여기에서 찾을 수 있습니다.

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

편집하다: UDF는 데이터베이스에 영향을 줄 수 없으므로 REGEX COM 객체를 등록 할 수 없으므로 해당 아이디어를 시전합니다. 그러나 저장된 절차는 경로 일 수 있습니다.

사례에 민감한 비교를하려면 쿼리가 사례에 민감하기 위해 Collation을 설정 한 다음 교체를 사용해야합니다. 여기에 당신을 지적하는 데 도움이 될 기사가 있습니다. 바른 길:

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의 가치를 무언가로 변경하면 테스트하고 싶습니다.

또한 @ARG +가 필요한 공간 수와 동일한 길이를 가진 문자열을 수용하기 위해 @Output 선언을 변경해야 할 수도 있습니다. 나는 내 예에서 그것을 두 배로 늘렸다.

나는 이것이 필요에 따라 정확히 작동한다는 것을 알았습니다. 예의 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