Finding the length of the string without STRLEN function:
I have seen a request in one of the forum on String Lenght function. Finding the string length with out STRLEN built-in function. Nice question right !!!
Solution:
Here is the solution,
1. First -> Replace all the empty character in the string to * or any other special character.
2. Second -> Check for each letter in the string for empty until it reaches the end.
3. Third -> The string will have an empty character in the end(By default)
4. Fourth -> Get the Count of the string.
**********************************************************************************
ALTER PROCEDURE dbo.LEN_WITHOUT_STRLEN_FUNCTION(@String VARCHAR(8000))
AS
BEGIN
DECLARE @CNT INT, @IDX INT, @Flag INT
SELECT @String = REPLACE(@String, ' ','*'), @CNT = 0, @IDX = 1, @Flag = 1
WHILE ( @Flag = 1 )
BEGIN
IF ((SELECT SUBSTRING(@String,@IDX,1)) <> '')
BEGIN
SET @CNT = @CNT + 1
END
ELSE
BEGIN
SET @Flag = 0
END
SET @IDX = @IDX + 1
END
PRINT @CNT
END
**********************************************************************************
EXEC LEN_WITHOUT_STRLEN_FUNCTION 'VENKATESAN PRABU'
**********************************************************************************
Cheers,
Venkatesan Prabu .J
No comments:
Post a Comment