|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Recursive USer-defined funstion problembut it doesn't work and I have no idea why. Code is below. Any help appreciated CREATE FUNCTION [dbo].[reps] (@string varchar) RETURNS int AS BEGIN declare @output int if len(@string) > 1 begin set @output = dbo.reps(right(@string,len(@string)-1)) if left(@string,1) = ',' begin set @output = 1 + @output end end else if len(@string)=1 begin if @string = ',' begin set @output =1 end else set @output =0 end return @output end Perhaps that might help you, its from Igor who presented these functions
here: CREATE function OCCURS (@cSearchExpression nvarchar(4000), @cExpressionSearched nvarchar(4000)) returns smallint as begin declare @start_location smallint, @occurs smallint select @start_location = dbo.CHARINDEX_BIN(@cSearchExpression, @cExpressionSearched, 1), @occurs = 0 while @start_location > 0 select @occurs = @occurs + 1, @start_location = dbo.CHARINDEX_BIN(@cSearchExpression, @cExpressionSearched, @start_location+1) return @occurs end GO -- Author: Igor Nikiforov, Montreal, EMail: u***@sympatico.ca Show quote "C_Sheffield" <CSheffi***@discussions.microsoft.com> schrieb im Newsbeitrag news:A5C56607-111A-4182-8984-465FF1003CA6@microsoft.com... >I have created a recursive function count the number of commas in a string, > but it doesn't work and I have no idea why. Code is below. Any help > appreciated > > CREATE FUNCTION [dbo].[reps] (@string varchar) > RETURNS int AS > BEGIN > declare @output int > > if len(@string) > 1 > begin > set @output = dbo.reps(right(@string,len(@string)-1)) > if left(@string,1) = ',' > begin > set @output = 1 + @output > end > end > else > if len(@string)=1 > begin > if @string = ',' > begin > set @output =1 > end > else > set @output =0 > end > > return @output > end Hi
The SQL Server limitation on nesting is 32, so your implimentation is not very useful for longer strings. What error are you getting? -- Show quote-------------------------------- Mike Epprecht, Microsoft SQL Server MVP Zurich, Switzerland MVP Program: http://www.microsoft.com/mvp Blog: http://www.msmvps.com/epprecht/ "C_Sheffield" wrote: > I have created a recursive function count the number of commas in a string, > but it doesn't work and I have no idea why. Code is below. Any help > appreciated > > CREATE FUNCTION [dbo].[reps] (@string varchar) > RETURNS int AS > BEGIN > declare @output int > > if len(@string) > 1 > begin > set @output = dbo.reps(right(@string,len(@string)-1)) > if left(@string,1) = ',' > begin > set @output = 1 + @output > end > end > else > if len(@string)=1 > begin > if @string = ',' > begin > set @output =1 > end > else > set @output =0 > end > > return @output > end Hi
If the string starts with a comma , I get a count of 1, otherwise I get 0, no matter how many commas are in the string I supply. select dbo.reps('test,test,test') returns 0 select dbo.reps(',test,test,test') returns 1 Thanks Chris Show quote "Mike Epprecht (SQL MVP)" wrote: > Hi > > The SQL Server limitation on nesting is 32, so your implimentation is not > very useful for longer strings. > > What error are you getting? > -- > -------------------------------- > Mike Epprecht, Microsoft SQL Server MVP > Zurich, Switzerland > > MVP Program: http://www.microsoft.com/mvp > > Blog: http://www.msmvps.com/epprecht/ > > > > "C_Sheffield" wrote: > > > I have created a recursive function count the number of commas in a string, > > but it doesn't work and I have no idea why. Code is below. Any help > > appreciated > > > > CREATE FUNCTION [dbo].[reps] (@string varchar) > > RETURNS int AS > > BEGIN > > declare @output int > > > > if len(@string) > 1 > > begin > > set @output = dbo.reps(right(@string,len(@string)-1)) > > if left(@string,1) = ',' > > begin > > set @output = 1 + @output > > end > > end > > else > > if len(@string)=1 > > begin > > if @string = ',' > > begin > > set @output =1 > > end > > else > > set @output =0 > > end > > > > return @output > > end On Wed, 18 May 2005 04:05:17 -0700, C_Sheffield wrote:
>I have created a recursive function count the number of commas in a string, Hi C,>but it doesn't work and I have no idea why. Code is below. Any help >appreciated (snip) You declare the @string argument as varchar, without length. The default length is 1. All remainging characters will be lost. BTW, you really don't need recursion for this: SELECT LEN(@string) - LEN(REPLACE(@string, ',' ,'') Best, Hugo -- (Remove _NO_ and _SPAM_ to get my e-mail address) Your way is a lot, lot better, but thanks also for solving why it didn't
work, it was driving me mad. Thanks again Chris Show quote "Hugo Kornelis" wrote: > On Wed, 18 May 2005 04:05:17 -0700, C_Sheffield wrote: > > >I have created a recursive function count the number of commas in a string, > >but it doesn't work and I have no idea why. Code is below. Any help > >appreciated > (snip) > > Hi C, > > You declare the @string argument as varchar, without length. The default > length is 1. All remainging characters will be lost. > > BTW, you really don't need recursion for this: > > SELECT LEN(@string) - LEN(REPLACE(@string, ',' ,'') > > Best, Hugo > -- > > (Remove _NO_ and _SPAM_ to get my e-mail address) > This will not work in all cases:
declare @string varchar(100) set @string = ', ,' (notice the space between the 2 commas) SELECT LEN(@string) - LEN(REPLACE(@string, ',' ,'')) ....returns 3 Show quote "Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:9ddm81dmplirtnmdlh72dss9d6rot49uk2@4ax.com... > On Wed, 18 May 2005 04:05:17 -0700, C_Sheffield wrote: > >>I have created a recursive function count the number of commas in a >>string, >>but it doesn't work and I have no idea why. Code is below. Any help >>appreciated > (snip) > > Hi C, > > You declare the @string argument as varchar, without length. The default > length is 1. All remainging characters will be lost. > > BTW, you really don't need recursion for this: > > SELECT LEN(@string) - LEN(REPLACE(@string, ',' ,'') > > Best, Hugo > -- > > (Remove _NO_ and _SPAM_ to get my e-mail address) On Wed, 18 May 2005 10:07:48 -0400, Raymond D'Anjou wrote:
>This will not work in all cases: Hi Raymond,> >declare @string varchar(100) >set @string = ', ,' (notice the space between the 2 commas) >SELECT LEN(@string) - LEN(REPLACE(@string, ',' ,'')) > >...returns 3 Good catch! This is caused by removal of the trailing space. It gets worse with more trailing spaces: declare @string varchar(100) set @string = 'abc ,' -- 5 spaces between abc and , SELECT LEN(@string) - LEN(REPLACE(@string, ',' ,'')) -- returns 6 And here's the corrected version: declare @string varchar(100) set @string = 'abc ,' -- 5 spaces between abc and , SELECT LEN(@string + 'X') - LEN(REPLACE(@string + 'X', ',' ,'')) -- 1 Best, Hugo -- (Remove _NO_ and _SPAM_ to get my e-mail address) What if we put markers on either end?
LEN('{' + @string + '}') - LEN(REPLACE('{' + @string + '}', ',' ,'')) - 2 On 18 May 2005 16:21:58 -0700, --CELKO-- wrote:
>What if we put markers on either end? Hi Joe,> >LEN('{' + @string + '}') - LEN(REPLACE('{' + @string + '}', ',' ,'')) >- 2 You don't need the -2, since you add the delimiters in both LEN(...) expressions. Further, there's no need for a delimiter at the start. SQL Server will remove trailing spaces, not leading spaces (as per the ANNSI standard, IIRC). After removing the error and removing the unnecessary first delimiter, this leaves your solution as: LEN(@string + '}') - LEN(REPLACE(@string + '}', ',' ,'')) And apart from the choice of delimiter ('}' vs 'X'), this is exactly the same as what I already posted in my corrected version. Best, Hugo -- (Remove _NO_ and _SPAM_ to get my e-mail address) Why recursivem this problem is easily solvced just by examining each
character in the string... CREATE FUNCTION dbo.reps (@string varchar) RETURNS int AS BEGIN declare @Count int Set @Count = 0 Declare @C Char(1) While Len(@string) > 1 Begin Select @C = Left(@string,1), @string = Substring(@string, 2,Len(@string) -1) If @C = ',' Set @Count = @Count + 1 End Return @Count End -- Show quoteCharles Bretana Jr. Arete Industries Inc. "C_Sheffield" wrote: > I have created a recursive function count the number of commas in a string, > but it doesn't work and I have no idea why. Code is below. Any help > appreciated > > CREATE FUNCTION [dbo].[reps] (@string varchar) > RETURNS int AS > BEGIN > declare @output int > > if len(@string) > 1 > begin > set @output = dbo.reps(right(@string,len(@string)-1)) > if left(@string,1) = ',' > begin > set @output = 1 + @output > end > end > else > if len(@string)=1 > begin > if @string = ',' > begin > set @output =1 > end > else > set @output =0 > end > > return @output > end Thanks, I just like using recursive scripts in programming, maybe they are
not that suited to SQL though Show quote "CBretana" wrote: > Why recursivem this problem is easily solvced just by examining each > character in the string... > > CREATE FUNCTION dbo.reps (@string varchar) > RETURNS int AS > BEGIN > declare @Count int Set @Count = 0 > > Declare @C Char(1) > While Len(@string) > 1 > Begin > Select @C = Left(@string,1), > @string = Substring(@string, 2,Len(@string) -1) > If @C = ',' Set @Count = @Count + 1 > End > Return @Count > End > > > > -- > Charles Bretana Jr. > Arete Industries Inc. > > > "C_Sheffield" wrote: > > > I have created a recursive function count the number of commas in a string, > > but it doesn't work and I have no idea why. Code is below. Any help > > appreciated > > > > CREATE FUNCTION [dbo].[reps] (@string varchar) > > RETURNS int AS > > BEGIN > > declare @output int > > > > if len(@string) > 1 > > begin > > set @output = dbo.reps(right(@string,len(@string)-1)) > > if left(@string,1) = ',' > > begin > > set @output = 1 + @output > > end > > end > > else > > if len(@string)=1 > > begin > > if @string = ',' > > begin > > set @output =1 > > end > > else > > set @output =0 > > end > > > > return @output > > end |
|||||||||||||||||||||||