Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Problem with Function...

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-07-31 : 11:49:03
Abhijit writes "I am writing a function called full soundex that finds soundex values of each word in the string seperated by semicolans. I am putting the function here:
------------------------------------------------------------
IF EXISTS (SELECT * 
FROM sysobjects
WHERE name = 'fn_FullSoundex') --N'<scalar_function_name, sysname, test_function>')
DROP FUNCTION fn_FullSoundex --<scalar_function_name, sysname, test_function>
GO

CREATE FUNCTION fn_FullSoundex --<scalar_function_name, sysname, test_function>
(@strIn CHAR(255)) --<@param1, sysname, @p1> <data_type_for_param1, , int>,
RETURNS VARCHAR(100) -- <function_data_type, ,int>
AS
BEGIN
DECLARE @OneWord AS VARCHAR(100)
DECLARE @iLn AS INT
DECLARE @iCount AS INT
DECLARE @cChr AS CHAR
DECLARE @ThisSoundex AS VARCHAR(100)
DECLARE @Soundex AS VARCHAR(100)

--PRINT @strIn
SET @iCount = 1
SET @iLn = LEN(@strIn)
SET @ThisSoundex = ''
SET @Soundex = ''
SET @OneWord = ''
WHILE (@iCount <= @iLn)
BEGIN
SET @cChr = SUBSTRING ( @strIn,@iCount,1)
IF (@cChr = ' ' )
BEGIN
-- SET @ThisSoundex = SOUNDEX (@OneWord)
SET @Soundex = @Soundex + SOUNDEX (@OneWord) + ';'
SET @OneWord = ''
END
ELSE
BEGIN
SET @OneWord = @OneWord + @cChr
END
SET @iCount = @iCount + 1
END
IF (@OneWord <> '')
BEGIN
SET @ThisSoundex = SOUNDEX (@OneWord)
SET @Soundex = @Soundex + @ThisSoundex + ';'
END
RETURN (@Soundex)
END
GO
------------------------------------------------------------
When I pass "MIRA VINODCHADRA SHAH" to the function it should return: "M600;V532;S000;", Instead it returns "M600;"

So I wrote a procedure with the same code and replaced RETURN by PRINT Giving the procedure also:
------------------------------------------------------------
IF EXISTS (SELECT name
FROM sysobjects
WHERE name = 'FullSoundex' -- '<procedure_name, sysname, proc_test>'
AND type = 'P')
DROP PROCEDURE FullSoundex -- <procedure_name, sysname, proc_test>
GO

CREATE PROCEDURE FullSoundex -- <procedure_name, sysname, proc_test>
(@strIn CHAR(255))
AS
DECLARE @OneWord AS VARCHAR(100)
DECLARE @iLn AS INT
DECLARE @iCount AS INT
DECLARE @cChr AS CHAR
DECLARE @ThisSoundex AS VARCHAR(5)
DECLARE @Soundex AS VARCHAR(100)

--PRINT @strIn
SET @iCount = 1
SET @iLn = LEN(@strIn)
SET @ThisSoundex = ''
SET @Soundex = ''
SET @OneWord = ''
WHILE (@iCount <= @iLn)
BEGIN
SET @cChr = SUBSTRING ( @strIn,@iCount,1)
IF (@cChr = ' ' )
BEGIN
SET @ThisSoundex = SOUNDEX (@OneWord)
SET @Soundex = @Soundex + @ThisSoundex + ';'
SET @OneWord = ''
END
ELSE
BEGIN
SET @OneWord = @OneWord + @cChr
END
SET @iCount = @iCount + 1
END
IF (@OneWord <> '')
BEGIN
SET @ThisSoundex = SOUNDEX (@OneWord)
SET @Soundex = @Soundex + @ThisSoundex + ';'
END
PRINT @Soundex

GO

------------------------------------------------------------
Now when I pass "MIRA VINODCHADRA SHAH" to this procedure It prints "M600;V532;S000;".

Can anybody guide me where is the mistake. Where am I going Wrong??? Due to this I am stuck very badly."
   

- Advertisement -