更新col1以使用SQL Server将col1中字符串中每个单词的第一个字母大写

时间:2016-07-13 09:15:41

标签: sql-server-2008

在搜索代码以大写SQL Server中字符串中每个单词的第一个字母后,我发现了这个:

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
GO 

但我现在不知道如何关联更新SSMS中的代码......就像这样

update tabel1 
set @InputString = @OutputString

2 个答案:

答案 0 :(得分:0)

你交叉申请

对外部查询的每一行执行交叉应用,因此假设tableyouwanttoupdate包含您想要大写的字符串,您可以传递它的功能并在更新中使用它

Update t1
set t1.string=b.string
from
tableyouwanttoupdate t1
cross apply
[dbo].[InitCap] (t1.string) b(string)

答案 1 :(得分:0)

这个怎么样:

DECLARE @tbl TABLE (ID INT IDENTITY,s NVARCHAR(100));
INSERT INTO @tbl(s) VALUES
 ('this is all lower case!')
,('Here we have a sentence. And another one!')
,('This IS mIxEd!!! CoMMpletelY MixeD!');

WITH Splitted AS
(
    SELECT ID
          ,s
          ,CAST(N'<x>' + REPLACE((SELECT s AS [*] FOR XML PATH('')),N' ',N'</x><x>') + N'</x>' AS XML) AS InParts
    FROM @tbl
)
SELECT ID
      ,s
      ,(
        STUFF(
        (
            SELECT ' ' + UPPER(LEFT(x.value('.','nvarchar(max)'),1)) + LOWER(SUBSTRING(x.value('.','nvarchar(max)'),2,1000))
            FROM Splitted.InParts.nodes('/x') AS A(x)
            FOR XML PATH('')
        ),1,1,'')
       ) AS NewString
FROM Splitted

结果

ID  s                                           NewString
1   this is all lower case!                     This Is All Lower Case!
2   Here we have a sentence. And another one!   Here We Have A Sentence. And Another One!
3   This IS mIxEd!!! CoMMpletelY MixeD!         This Is Mixed!!! Commpletely Mixed!

更新

如果您想更新专栏,这也很容易:

DECLARE @tbl TABLE (ID INT IDENTITY,s NVARCHAR(100));
INSERT INTO @tbl(s) VALUES
 ('this is all lower case!')
,('Here we have a sentence. And another one!')
,('This IS mIxEd!!! CoMMpletelY MixeD!');

WITH Splitted AS
(
    SELECT ID
          ,s
          ,CAST(N'<x>' + REPLACE((SELECT s AS [*] FOR XML PATH('')),N' ',N'</x><x>') + N'</x>' AS XML) AS InParts
    FROM @tbl
)
UPDATE Splitted SET s=
     (
        STUFF(
        (
            SELECT ' ' + UPPER(LEFT(x.value('.','nvarchar(max)'),1)) + LOWER(SUBSTRING(x.value('.','nvarchar(max)'),2,1000))
            FROM Splitted.InParts.nodes('/x') AS A(x)
            FOR XML PATH('')
        ),1,1,'')
       ) 
FROM Splitted;

SELECT * FROM @tbl;