是否有任何SQL Server内置函数在驼峰情况下转换字符串?

gir*_*ish 11 sql sql-server-2008

如果SQL Server中已存在任何功能,我不想为此创建自定义功能

输入字符串:This is my string to convert
预期输出:This Is My String To Convert

ash*_*lia 24

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[InitCap] ( @InputString varchar(4000) ) 
RETURNS VARCHAR(4000)
AS
BEGIN

DECLARE @Index          INT
DECLARE @Char           CHAR(1)
DECLARE @PrevChar       CHAR(1)
DECLARE @OutputString   VARCHAR(255)

SET @OutputString = LOWER(@InputString)
SET @Index = 1

WHILE @Index <= LEN(@InputString)
BEGIN
    SET @Char     = SUBSTRING(@InputString, @Index, 1)
    SET @PrevChar = CASE WHEN @Index = 1 THEN ' '
                         ELSE SUBSTRING(@InputString, @Index - 1, 1)
                    END

    IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')
    BEGIN
        IF @PrevChar != '''' OR UPPER(@Char) != 'S'
            SET @OutputString = STUFF(@OutputString, @Index, 1, UPPER(@Char))
    END

    SET @Index = @Index + 1
END

RETURN @OutputString

END



Declare @str nvarchar(100)
SET @str = 'my string to convert'
SELECT @str = [dbo].[InitCap](@str)
SELECT @str 
Run Code Online (Sandbox Code Playgroud)


sgo*_*les 7

AFAIK,SQL Server没有内置功能.
你必须为它编写自定义函数.

试试这个.

CREATE FUNCTION [dbo].[CamelCase]
(@Str varchar(8000))
RETURNS varchar(8000) AS
BEGIN
  DECLARE @Result varchar(2000)
  SET @Str = LOWER(@Str) + ' '
  SET @Result = ''
  WHILE 1=1
  BEGIN
    IF PATINDEX('% %',@Str) = 0 BREAK
    SET @Result = @Result + UPPER(Left(@Str,1))+
    SubString  (@Str,2,CharIndex(' ',@Str)-1)
    SET @Str = SubString(@Str,
      CharIndex(' ',@Str)+1,Len(@Str))
  END
  SET @Result = Left(@Result,Len(@Result))
  RETURN @Result
END  
Run Code Online (Sandbox Code Playgroud)

输出:

Input String    : 'microSoft sql server'
Output String   : 'Microsoft Sql Server'
Run Code Online (Sandbox Code Playgroud)


Vin*_*rgh 6

我必须选择"不,那不存在".这是基于几年来仔细阅读T-SQL中的可用字符串函数以及SQL Server 2008 R2中最近的一些为期5天的课程.

当然,我还是错了:).


Mik*_*Vee 5

如果您的操作的目标是美化名称字符串,则可以将正确的大写定义为每个单词的第一个字母,由非字母字符分隔。

其他解决方案不考虑:

  1. 保留间距(尤其是尾随空格)。
  2. 保留 NULL、空字符串或仅包含空格的字符串。
  3. 处理的不仅仅是空格(例如破折号、逗号、下划线等...)
  4. 在单词/标记之间处理多个非字母字符。
  5. 处理异常(例如“James William Bottomtooth the III”中的 McDonald 或 III)。

注意:我的解决方案不处理异常。
如果您非常关心这些,那么我建议为那些编写 CLR C# 程序集,因为它会很棘手,而字符串是 C# 擅长的领域。
这里的另一个解决方案试图解决这个问题,但它仍然需要“ ivan可怕的iv ”并输出“**IV***an Terrible The IV*”。

这是我想出的功能:

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fs_PascalCase]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fs_PascalCase]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[fs_PascalCase]
(
    @Text nVarChar(MAX)
)
RETURNS nVarChar(MAX)
AS
BEGIN
        SET @Text = LOWER(@Text)--This step is optional.  Keep if you want the code below to control all casing. - 11/26/2013 - MCR.
    DECLARE @New nVarChar(MAX) = (CASE WHEN @Text IS NULL THEN NULL ELSE '' END)--Still return null when source is null. - 11/26/2013 - MCR.
    DECLARE @Len   Int = LEN(REPLACE(@Text, ' ', '_'))--If you want to count/keep trailing-spaces, you MUST use this!!! - 11/26/2013 - MCR.
    DECLARE @Index Int = 1--Sql-Server is 1-based, not 0-based.
    WHILE (@Index <= @Len)
        IF (SUBSTRING(@Text, @Index, 1) LIKE '[^a-z]' AND @Index + 1 <= @Len)--If not alpha and there are more character(s).
            SELECT @New = @New + UPPER(SUBSTRING(@Text, @Index, 2)), @Index = @Index + 2
        ELSE
            SELECT @New = @New +       SUBSTRING(@Text, @Index, 1) , @Index = @Index + 1

    --If @Text is null, then @Len will be Null, and everything will be null.
    --If @Text is '',   then (@Len - 1) will be -1, so ABS() it to use 1 instead, which will still return ''.
    RETURN ( UPPER(LEFT(@New, 1)) + RIGHT(@New, ABS(@Len - 1)) )
END
GO
Run Code Online (Sandbox Code Playgroud)


你会这样称呼它:

SELECT dbo.fs_PascalCase(NULL)[Null],
       dbo.fs_PascalCase('')[EmptyString],
       dbo.fs_PascalCase('hello   how are-you TODAY    ')[LongString]
Run Code Online (Sandbox Code Playgroud)


输出将如下所示:

大小写输出