【问题标题】:How to capitalize first letter each words in sql server?如何将sql server中每个单词的首字母大写?
【发布时间】:2015-10-28 02:43:13
【问题描述】:

例如,我的表 Student 带有字段 Name,我想将字段 Name 中的“jason mraz”更改为“Jason Mraz”。 SQL Server 中的语法如何?

感谢您的回答。

【问题讨论】:

标签: sql-server sql-server-2012


【解决方案1】:

这种语法的名称是“Camel Case”

看起来可能已经有解决方案@@is there any sql server built-in function to convert string in camel case?

【讨论】:

  • 避免仅链接答案
【解决方案2】:
CREATE FUNCTION [dbo].[CapitalizeFirstLetter]
(
--string need to format
@string VARCHAR(200)--increase the variable size depending on your needs.
)
RETURNS VARCHAR(200)
AS

BEGIN
--Declare Variables
DECLARE @Index INT,
@ResultString VARCHAR(200)--result string size should equal to the @string variable size
--Initialize the variables
SET @Index = 1
SET @ResultString = ''
--Run the Loop until END of the string

WHILE (@Index <LEN(@string)+1)
BEGIN
IF (@Index = 1)--first letter of the string
BEGIN
--make the first letter capital
SET @ResultString =
@ResultString + UPPER(SUBSTRING(@string, @Index, 1))
SET @Index = @Index+ 1--increase the index
END

-- IF the previous character is space or '-' or next character is '-'

ELSE IF ((SUBSTRING(@string, @Index-1, 1) =' 'or SUBSTRING(@string, @Index-1, 1) ='-' or SUBSTRING(@string, @Index+1, 1) ='-') and @Index+1 <> LEN(@string))
BEGIN
--make the letter capital
SET
@ResultString = @ResultString + UPPER(SUBSTRING(@string,@Index, 1))
SET
@Index = @Index +1--increase the index
END
ELSE-- all others
BEGIN
-- make the letter simple
SET
@ResultString = @ResultString + LOWER(SUBSTRING(@string,@Index, 1))
SET
@Index = @Index +1--incerase the index
END
END--END of the loop

IF (@@ERROR
<> 0)-- any error occur return the sEND string
BEGIN
SET
@ResultString = @string
END
-- IF no error found return the new string
RETURN @ResultString
END

【讨论】:

    猜你喜欢
    • 2012-07-24
    • 2015-11-10
    • 2021-08-08
    • 2020-08-30
    • 2015-11-04
    • 2012-11-11
    • 1970-01-01
    • 2014-05-19
    • 2010-12-05
    相关资源
    最近更新 更多