2013-03-08 162 views
52

我需要一個SQL語句來大寫每個單詞的首字母。其他字符必須小寫。SQL:僅首字母大寫

的話可以是這樣的:

wezembeek-oppem 
roeselare 
BRUGGE 
louvain-la-neuve 

這將是:

Wezembeek-Oppem 
Roeselare 
Brugge 
Louvain-La-Neuve 

這應該是用一個UPDATE語句,我想更新一列的數據。 非常感謝您提前給出答案,我是一名SQL新手。

+4

看看這有助於http://stackoverflow.com/q/55054/285582 – bruno 2013-03-08 09:41:04

+0

謝謝,布魯諾。您的鏈接提供瞭解決方案。 – samn 2013-03-08 10:08:30

回答

108

您是要求重命名列本身還是大寫列中的數據?如果它的數據你已經改變,那麼使用這個:

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] 

希望這有助於。

編輯:我意識到' - '所以這裏是我的嘗試來解決這個問題的功能。

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]) 
+3

這不會處理連字符的情況。 – Bridge 2013-03-08 09:37:49

+0

是的,它將是現有列的更新,我將其添加到我的最初問題。 – samn 2013-03-08 09:39:30

+0

增加了一個嘗試的答案來處理連字符的情況 - 沒有測試,因爲我在工作,所以它的一個快速嘗試,可能需要調整。 – 2013-03-08 10:01:16

0
select replace(wm_concat(new),',','-') exp_res from (select distinct initcap(substr(name,decode(level,1,1,instr(name,'-',1,level-1)+1),decode(level,(length(name)-length(replace(name,'-','')))+1,9999,instr(name,'-',1,level)-1-decode(level,1,0,instr(name,'-',1,level-1))))) new from table; 
connect by level<= (select (length(name)-length(replace(name,'-','')))+1 from table)); 
+0

不是SQL Server的答案... – 2013-03-08 09:48:24

+0

雅我沒有看到指定的數據庫!我的錯誤.... 該查詢應該是爲Oracle數據庫.. – Aspirant 2013-03-08 09:58:22

6

創建以下功能

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 ; 

然後調用它像

select dbo.InitialCap(columnname) from yourtable 
+0

您應該[屬性您的代碼源](https://www.sqlservercentral.com/Forums/FindPost993409.aspx)。 – 2017-12-07 11:07:53

3

請檢查查詢,而無需使用功能:

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