CREATE FUNCTION dbo.GetLength(@InputString VARCHAR(8000))RETURNS INTAS/* -- Comments --Created By: Alwyn Duraisingh.MCreated on: April 26 2010 10:10 PM Purpose: To find the length of the string without using LEN() function*/BEGIN DECLARE @Count INT, @Index INT, @Flag INT SELECT @InputString = REPLACE(@InputString, ' ','*'), @Count = 0, @Index = 1, @Flag = 1 --Looping through each and every character and count one by one WHILE ( @Flag = 1 ) BEGIN IF ((SELECT SUBSTRING(@InputString,@Index,1)) <> '') BEGIN SET @Count = @Count + 1 END ELSE BEGIN SET @Flag = 0 END SET @Index = @Index + 1 END RETURN @CountEND
SELECT dbo.GetLength('God is only one') AS LENGTHRESULT:*******LENGTH*******15