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

可以这样说:

wezembeek-oppem
roeselare
BRUGGE
louvain-la-neuve

这必须是:

Wezembeek-Oppem
Roeselare
Brugge
Louvain-La-Neuve

这应该是一个UPDATE语句,我想更新一列的数据.

推荐答案

您是要求重命名列本身还是将列中的数据资本化?如果您必须更改其数据,请使用以下方法:

UPDATE [yourtable]
SET word=UPPER(LEFT(word,1))+LOWER(SUBSTRING(word,2,LEN(word)))

如果只是为了显示而更改,不需要更改表中的实际数据:

SELECT UPPER(LEFT(word,1))+LOWER(SUBSTRING(word,2,LEN(word))) FROM [yourtable]

希望这有帮助.

EDIT: I realised about the '-' so here is my attempt to solve this problem in a function.

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

那么代码就是:

UPDATE [yourtable]
SET word=dbo.CapitalizeFirstLetter([STRING TO GO HERE])

Sql相关问答推荐

识别顺序记录组

为主表中的每一行查找N个最新行

在Postgres中实现合并功能的干净方法,因为当目标/源不匹配时

从自定义日期和时间开始,每月具有给定状态的公司数量

如何在snowflake中进行SQL的反向填充

如何利用单列历史SQLsnowflake获得合并结果

Ffltter&;Dart SQL Lite包:是否可以在一个查询中执行多条更新语句(每次执行不同的WHERE参数)

如何查找所提供日期范围的所有季度开始日期和结束日期

OVER子句WITH PARTITION BY和ORDER BY忽略主查询的WHERE子句

直接加法(1+1)与聚合函数SUM(1+1)的区别是什么

在一个子查询中签入ID';S,如果未返回,则签入另一个子查询

如何根据行状态设置正确的标志

日期逻辑(查找过go 90 天内的第一个匹配行)

根据要过滤的列的值进行联接和分组

如何在 JSONB 数组的每个对象中添加新的键值对- PostgreSQL

PostgreSQL中如何提取以特定字符开头的字符串中的所有单词?

使用其他表 SUM 的交换价格转换价格并获得 AVG

获取 SQL Server 中每一行的两个-之间的文本

在 SQL 中使用循环遍历按时间顺序排列的数据

如何对 SQL 表中的连续时间戳进行分组?