2013-03-08 16 views
52

Ho bisogno di un'istruzione SQL per scrivere in maiuscolo la prima lettera di ogni parola. Gli altri personaggi devono essere in minuscolo.SQL: capitalizza solo la prima lettera

Le parole possono essere in questo modo:

wezembeek-oppem 
roeselare 
BRUGGE 
louvain-la-neuve 

che avrebbe dovuto essere:

Wezembeek-Oppem 
Roeselare 
Brugge 
Louvain-La-Neuve 

Questo dovrebbe essere con un'istruzione UPDATE, voglio aggiornare i dati di una colonna. Grazie mille per le vostre risposte in anticipo, sono un debuttante di SQL.

+4

vedere se questo aiuta http://stackoverflow.com/q/55054/285582 – bruno

+0

Grazie, bruno. Il tuo link ha fornito la soluzione. – samn

risposta

108

Stai chiedendo la ridenominazione della colonna stessa o la capitalizzazione dei dati all'interno della colonna? Se i suoi dati che hai per cambiare, quindi utilizzare questo:

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

Se si voleva solo cambiare solo per la visualizzazione e non hanno bisogno i dati effettivi in ​​tabella per modificare:

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

Speranza questo aiuta.

EDIT: mi sono reso conto circa il '-' ecco il mio tentativo di risolvere questo problema in una funzione.

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 

Così allora il codice sarebbe:

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

Questo non gestirà i casi con trattino. – Bridge

+0

sì, sarebbe un aggiornamento di una colonna esistente, l'ho aggiunto alla mia domanda iniziale. – samn

+0

ha aggiunto un tentativo di risposta per gestire casi con trattino - non testati poiché sono al lavoro, quindi è un tentativo veloce, potrebbe essere necessario un ritocco. –

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

Non è una risposta SQL Server ... –

+0

ya non ho visto il db specificato !! errore mio ... La query dovrebbe essere per Oracle db .. – Aspirant

6

creare la funzione di sotto

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 ; 

Poi lo chiamano come

select dbo.InitialCap(columnname) from yourtable 
+0

È necessario [attribuire le origini del codice] (https://www.sqlservercentral.com/Forums/FindPost993409.aspx). –

3

Si prega di verificare la query senza l'utilizzo di una funzione:

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 
Problemi correlati