Skip to content

Instantly share code, notes, and snippets.

@rodolfofadino
Created October 30, 2012 17:30
Show Gist options
  • Save rodolfofadino/3981714 to your computer and use it in GitHub Desktop.
Save rodolfofadino/3981714 to your computer and use it in GitHub Desktop.
UperFirst
/****** Object: UserDefinedFunction [dbo].[UperFirst] Script Date: 10/30/2012 15:27:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
Create FUNCTION [dbo].[UperFirst]( @InputString varchar(max))
RETURNS varchar(max)
AS
BEGIN
DECLARE @Index INT
DECLARE @Char CHAR(1)
DECLARE @PrevChar CHAR(1)
DECLARE @OutputString VARCHAR(max)
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 != ''''
SET @OutputString = STUFF(@OutputString, @Index, 1, UPPER(@Char))
END
SET @Index = @Index + 1
END
Return @OutputString
END
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment