Función para convertir a nombre propio Nombres escritos en mayúscula en TSQL

 


En excel  el texto tenga el formato de un Nombre Propio, es decir, que cada palabra empiece con mayúscula, se debe utilizar la fórmula =NOMPROPIO(texto), y seleccionar la celda de texto a cambiar.


En TSQL podemos usar la siguiente función



SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

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(4000)


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 (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')

        SET @OutputString = STUFF(@OutputString, @Index, 1, UPPER(@Char))


    SET @Index = @Index + 1

END


RETURN @OutputString


END




/****** Script para actualizar ******/


     Update [BIOCOMMINIHC].[dbo].[Pacientes]

SET [Apellido] = [dbo].[InitCap]([Apellido]),

[Nombre]= [dbo].[InitCap]([Nombre])



Comentarios

Entradas populares de este blog

¿Qué es la JCAHO Joint Commission on Accreditation of Healthcare Organizations?

PARSEO DEL CODIGO PDF417 DEL DNI ARGENTINO

¿Como instalar El Cliente de SOPHOS VPN ?