Convert String to Sentence Case Script


This small function from Subhan Munshi converts a submitted string to sentence case.


CREATE FUNCTION fn_ToSentenceCase (@inputstr as varchar(1000))
RETURNS varchar(1000)
AS
BEGIN
	--Author: Subhan Munshi
	declare @Upper as char(1)
	declare @len as int
	declare @count as int
	declare @flag bit
	declare @resultstr varchar(100)
	
	set @len = len(@inputstr)
	set @count = 2
	set @resultstr = upper(left(@inputstr,1))
	while @count <= @len
	begin	
		select @Upper = substring(@inputstr,@count-1,1)
		if @Upper = ' '
			set @flag = 1
		else
			set @flag = 0
	
		if @flag = 1
			select @resultstr = @resultstr + upper(substring(@inputstr,@count,1))
		else
			select @resultstr = @resultstr + lower(substring(@inputstr,@count,1))
		set @count = @count + 1 
	end 	
	
	RETURN (@resultstr)
END

Get the Free Newsletter!

Subscribe to Cloud Insider for top news, trends & analysis

Latest Articles