Tuesday, 21 May 2013

Sql server::String with initial letter in uppercase.

string functions

Sql server::String with initial letter in uppercase.

    Hi, 
        Some time we need to select a string with first later is Uppercase,I mean Initial letter is Uppercase.
in that case a following function helf to you.
             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(255)
            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 != '''' or upper (@Char) != 'S'
            set @OutputString = stuff (@OutputString, @Index, 1, upper(@Char))
            end
            set @Index = @Index + 1
            end
            erturn @OutputString
            end

SELECT    [dbo].[InitCap] ('SANDIP GAIKWAD AITAE DFFDDF FSDFSFS')

1 comment:

8451831077