Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If my string is: "hi how to find the largest substring"
My output should be: The largest of the substring is--> substring

I have created a procedure, it works fine but it requires a space at the end of my string.
Thanks in advance if u suggest any procedure that could override this procedure to get the required output.
SQL
alter procedure largerSubstring @baseString varchar(250)
AS
begin
	declare @subString varchar(max),@len int,@largestSubString varchar(max),@prevLength int,@originalLength int
	set @prevLength=0
	set @originalLength=len(@baseString)
	while(@originalLength>0)
	BEGIN
		set @subString=left(@baseString,charindex(' ',@baseString)-1)
		if(len(@subString)>@prevLength)
		begin
			set @prevLength=len(@subString)
			set @largestSubString=@subString;
		end	
		set @baseString=substring(@baseString,charindex(' ',@baseString)+1,@originalLength)

		set @originalLength=@originalLength-(len(@subString)+1)
	END
	print 'the largest string length is--> '+convert(varchar,@prevLength)
	print 'the largest susstring is-->'+@largestSubString
end
Posted
Updated 20-Feb-12 19:00pm
v3
Comments
Nilesh Patil Kolhapur 20-Feb-12 23:53pm    
Hi teju,
your class rank trigger not working properly check it out.
Nilesh Patil Kolhapur 20-Feb-12 23:54pm    
post your procedure to get better answer.
The Doer 21-Feb-12 0:24am    
Nilesh it that class rank trigger works for me.., i have put 2 triggers over there.., in which u found difficulty?
The Doer 21-Feb-12 0:33am    
I have posted my procedure..,
as i am setting the substring value as seperated by 'spaces' hence it requires a space at the end of the string.
set @subString=left(@baseString,charindex(' ',@baseString)-1)
AmitGajjar 21-Feb-12 1:00am    
if you need space at the end of string then place it why you worry more to find other solution ?

Try this

SQL
alter procedure largerSubstring
    @baseString varchar(250)
AS
begin

    declare @subString          varchar(max),
            @len                int,
            @largestSubString   varchar(max),
            @prevLength         int,
            @foundIndex         int
    
    set @baseString         = isnull(rtrim(ltrim(@baseString)),'')
    set @prevLength         = 0
    set @largestSubString   = @baseString
    
    while(len(@baseString)>0)
    begin
        set @foundIndex = charindex(' ',@baseString)
        if @foundIndex = 0 -- not found
            begin
            set @subString  = @baseString
            set @baseString = ''
            end
        else
            begin
	    set @subString  = left(@baseString,@foundIndex-1)
	    set @baseString = ltrim(right(@baseString,len(@baseString)-@foundIndex))
            end

	 if(len(@subString)>@prevLength)
	   begin
	   set @prevLength         = len(@subString)
	   set @largestSubString   = @subString;
	   end	
    end
    print 'the largest string length is--> '+convert(varchar,@prevLength)
    print 'the largest susstring is-->'+@largestSubString

end
 
Share this answer
 
v2
Hi,

Here is the another version,

SQL
CREATE PROCEDURE [dbo].[usp_GetLengthyString] 
(   
 @sInputString VARCHAR(8000) 
) AS
BEGIN
DECLARE @sItem VARCHAR(8000)
DECLARE @lengthyString VARCHAR(8000)
DECLARE @sDelimiter VARCHAR 
SET @sDelimiter = ' '
DECLARE @List TABLE (string VARCHAR(8000), leng INT)
WHILE CHARINDEX(@sDelimiter,@sInputString,0) <> 0 
BEGIN 
	SELECT  @sItem=RTRIM(LTRIM(SUBSTRING(@sInputString,1,CHARINDEX(@sDelimiter,@sInputString,0)-1))), 
			@sInputString=RTRIM(LTRIM(SUBSTRING(@sInputString,CHARINDEX(@sDelimiter,@sInputString,0)+LEN(@sDelimiter),LEN(@sInputString))))
	IF LEN(@sItem) > 0 
		INSERT INTO @List 
		SELECT @sItem, LEN(@sItem)
END
IF LEN(@sInputString) > 0 
	INSERT INTO @List 
	SELECT @sInputString, LEN(@sInputString) -- Put the last item in

SELECT @lengthyString = string FROM @List ORDER BY leng ASC

PRINT 'The longest sub string is ' + @lengthyString

END
 
Share this answer
 
Hey,
if U Need Space at end of string then press Alt+255 it inserted blank Character.
Good Luck
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900