Invalid length parameter error using SUBSTRING - SQL Server 2008 -


in first place, sorry amount of code i'm going post! :( stuck. have tried many things , none seems work. don't know error is...

i going explain function does. converts time values custom format seconds. if want know more, value represents duration of arrhytmic episode detected pacemaker. example, if duration 2 minutes , 35 seconds, custom format 'pt2m35s', , it's string.

my function (which created in vba , i'm trying migrate sql server) reads text char char, , depending of char different calculations. here have code converted sql function:

alter function pttoseconds(@duration varchar(30)) returns float begin     declare @posletrareciente int     declare @haycoma int     declare @poscoma int      set @posletrareciente = 1     set @haycoma = 0     set @poscoma = 0      declare @texto varchar(30)     set @texto = @duration      declare @tiempototal float     declare @factortiempo float     declare @incremento float      set @tiempototal = 0.0     set @factortiempo = 0     set @incremento = 0      declare @caracter varchar(30)     declare @counter int     set @counter = 1      declare @longtexto int     set @longtexto = len(@duration)      while @counter < @longtexto         begin         set @caracter = substring(@texto,@counter, 1)          set @haycoma = case @caracter                 when '.' 1                 else 0                 end          set @factortiempo = case                      when @caracter = 'd' 86400                     when @caracter = 'h' 3600                     when @caracter = 'm' 60                     when (@caracter = 's' , @haycoma = 1) (1 / (power(10,(@counter - (@poscoma + 1)))))                     when (@caracter = 's' , @haycoma = 0) 1                     when @caracter = 't' 0                     when @caracter = '.' 1                     else 0         end          set @posletrareciente = @counter          set @incremento = case                 when @caracter = 'd' cast((substring(@texto, (@posletrareciente + 1), @counter - (@posletrareciente + 1))) float) * @factortiempo                 when @caracter = 'h' cast((substring(@texto, (@posletrareciente + 1), @counter - (@posletrareciente + 1))) float) * @factortiempo                 when @caracter = 'm' cast((substring(@texto, (@posletrareciente + 1), @counter - (@posletrareciente + 1))) float) * @factortiempo                 when (@caracter = 's' , @haycoma = 1) cast((substring(@texto, (@posletrareciente + 1), @counter - (@posletrareciente + 1))) float) * @factortiempo                 when (@caracter = 's' , @haycoma = 0) cast((substring(@texto, (@posletrareciente + 1), @counter - (@posletrareciente + 1))) float) * @factortiempo                 when @caracter = 't' cast((substring(@texto, (@posletrareciente + 1), @counter - (@posletrareciente + 1))) float) * @factortiempo                 when @caracter = '.' cast((substring(@texto, (@posletrareciente + 1), @counter - (@posletrareciente + 1))) float) * @factortiempo                 else @factortiempo         end          set @tiempototal = @tiempototal + @incremento         set @counter = @counter + 1      end      return @tiempototal end 

the error appears when calling substring. says following:

invalid length parameter passed left or substring function. 

and don't know can causing it. tried use substring separatedly in query:

select x = substring('pt3m44s',2,1) 

and works fine. mean error comes variable @counter, don't know why.

any orientation welcome.

thanks! i've tried specific can. if think haven't, please kind , tell why!

ignacio

you need rewrite logic.

i searched @posletrareciente in code , found this: set @posletrareciente = @counter

and expression @counter - (@posletrareciente + 1) gives -1.

and pass -1 third parameter in substring invalid


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -