SQL:仅限首字母大写

sam*_*amn 83 sql sql-server

我需要一个SQL语句来大写每个单词的第一个字母.其他字符必须是小写.

这些话可以是这样的:

wezembeek-oppem
roeselare
BRUGGE
louvain-la-neuve
Run Code Online (Sandbox Code Playgroud)

必须是:

Wezembeek-Oppem
Roeselare
Brugge
Louvain-La-Neuve
Run Code Online (Sandbox Code Playgroud)

这应该是UPDATE语句,我想更新列的数据.非常感谢你提前给出的答案,我是一个SQL新手.

Sco*_*ers 163

您是要求重命名列本身还是要将列中的数据大写?如果要更改其数据,请使用以下命令:

UPDATE [yourtable]
SET word=UPPER(LEFT(word,1))+LOWER(SUBSTRING(word,2,LEN(word)))
Run Code Online (Sandbox Code Playgroud)

如果您只想将其更改为仅显示,并且不需要更改表中的实际数据:

SELECT UPPER(LEFT(word,1))+LOWER(SUBSTRING(word,2,LEN(word))) FROM [yourtable]
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

编辑:我意识到' - '所以这是我尝试在函数中解决这个问题.

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
Run Code Online (Sandbox Code Playgroud)

那么代码将是:

UPDATE [yourtable]
SET word=dbo.CapitalizeFirstLetter([STRING TO GO HERE])
Run Code Online (Sandbox Code Playgroud)

  • 这不会处理带连字符的情况. (3认同)
  • 我实际上在这里找到了解决方案 http://stackoverflow.com/questions/55054/whats-the-best-way-to-capitalise-the-first-letter-of-each-word-in-a-string-in-s但你的解决方案非常好类似.谢谢! (3认同)

Dev*_*ion 11

创建以下功能

Alter FUNCTION InitialCap(@String VARCHAR(8000))
                  RETURNS VARCHAR(8000)
                 AS
 BEGIN 

                   DECLARE @Position INT;

SELECT @String   = STUFF(LOWER(@String),1,1,UPPER(LEFT(@String,1))) COLLATE Latin1_General_Bin,
                    @Position = PATINDEX('%[^A-Za-z''][a-z]%',@String COLLATE Latin1_General_Bin);

                    WHILE @Position > 0
                    SELECT @String   = STUFF(@String,@Position,2,UPPER(SUBSTRING(@String,@Position,2))) COLLATE Latin1_General_Bin,
                    @Position = PATINDEX('%[^A-Za-z''][a-z]%',@String COLLATE Latin1_General_Bin);

                     RETURN @String;
  END ;
Run Code Online (Sandbox Code Playgroud)

然后把它称为

select dbo.InitialCap(columnname) from yourtable
Run Code Online (Sandbox Code Playgroud)

  • 您应该[归因于您的代码源](https://www.sqlservercentral.com/Forums/FindPost993409.aspx). (4认同)

Tec*_*hDo 5

请在不使用函数的情况下检查查询:

declare @T table(Insurance varchar(max))

insert into @T values ('wezembeek-oppem')
insert into @T values ('roeselare')
insert into @T values ('BRUGGE')
insert into @T values ('louvain-la-neuve')

select (
       select upper(T.N.value('.', 'char(1)'))+
                lower(stuff(T.N.value('.', 'varchar(max)'), 1, 1, ''))+(CASE WHEN RIGHT(T.N.value('.', 'varchar(max)'), 1)='-' THEN '' ELSE ' ' END)
       from X.InsXML.nodes('/N') as T(N)
       for xml path(''), type
       ).value('.', 'varchar(max)') as Insurance
from 
  (
  select cast('<N>'+replace(
            replace(
                Insurance, 
                ' ', '</N><N>'),
            '-', '-</N><N>')+'</N>' as xml) as InsXML
  from @T
  ) as X
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

291775 次

最近记录:

9 年,8 月 前