Luca Bovo


Sql Server..... questo "sconosciuto"
Ora e Data
Calendario
aprile 2024
lmmgvsd
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345
Mappa
Statistiche
  • Views Home Page: 55.840
  • Views Posts: 47.235
  • Views Gallerie: 888
  • n° Posts: 21
  • n° Commenti: 21

Funzione che ritorna una stringa con UPPER della prima lettera di ogni parola.

La funzione sotto riportata restituisce una stringa con le maiuscole nel posto giusto.
Se la Stringa digitata è 'luCA bOVo', xxx_fn_CorrectCase('luCA bOVo') restituisce 'Luca Bovo'.
La variabile @Delim contiene i separatori di cui la funzione deve tenere conto per stabilire l'inizio della parola successiva.


CREATE FUNCTION xxx_fn_CorrectCase
(
--Stringa da convertire nel modo corretto
@vcIn varchar(8000)
)
--La funzione ritorna la stringa formattata correttamente
RETURNS varchar(8000)
AS
BEGIN
 IF @vcIn IS NULL 
 BEGIN
  --Ritorna NULL se l'input è NULL
  RETURN NULL
 END
 
 DECLARE @vcOut varchar(8000)
 DECLARE @i int 
 DECLARE @len int
 DECLARE @found_at int
 DECLARE <CASE_a int
 DECLARE <CASE_z int
 DECLARE @UCASE_A int
 DECLARE @UCASE_Z int
 DECLARE @Delim char(5)

 SET @i = 1
 SET @len = LEN(@vcIn)
 SET @vcOut = ''
 SET <CASE_a = 97
 SET <CASE_z = 122
 SET @Delim = ' ,-'''
 SET @UCASE_A = 65
 SET @UCASE_Z = 90
 
 WHILE @i <= @len
 BEGIN
  --Questo per tenere conto degli spazi
  WHILE CHARINDEX(SUBSTRING(@vcIn,@i,1), @Delim) > 0
  BEGIN
   SET @vcOut = @vcOut + SUBSTRING(@vcIn,@i,1)
   SET @i = @i + 1
  END
  IF ASCII(SUBSTRING(@vcIn,@i,1)) BETWEEN <CASE_a AND <CASE_z
  BEGIN
   --Converte ilPrimo carattere in Upper
   SET @vcOut = @vcOut + UPPER(SUBSTRING(@vcIn,@i,1))
  END
  ELSE
  BEGIN
   SET @vcOut = @vcOut + SUBSTRING(@vcIn,@i,1)
  END
  
  SET @i = @i + 1
  WHILE CHARINDEX(SUBSTRING(@vcIn,@i,1), @Delim) = 0 AND (@i <= @len)
  BEGIN
   IF ASCII(SUBSTRING(@vcIn,@i,1)) BETWEEN @UCASE_A AND @UCASE_Z
   BEGIN
    SET @vcOut = @vcOut + LOWER(SUBSTRING(@vcIn,@i,1))
   END
   ELSE
   BEGIN
    SET @vcOut = @vcOut + SUBSTRING(@vcIn,@i,1)
   END
   SET @i = @i + 1
  END
  
 END
RETURN @vcOut
END

Categoria: Script T-SQL
martedì, 27 set 2005 Ore. 09.44
My transcript

Transcript ID 675419
Access code Ivrea130




Archivio Posts
Anno 2010

Anno 2009

Anno 2008

Anno 2007

Anno 2006

Anno 2005
Copyright © 2002-2007 - Blogs 2.0
dotNetHell.it | Home Page Blogs
ASP.NET 2.0 Windows 2003