sql - Count how many times an integer is appears in a column -
i'm attempting find way count end of integer , display trailing value. example, have following table:
credit ====== 1051000 10000 2066 16000
i'd able count amount of times '0' appears , end result following
credit cntoccurancevals ====== ====== 1051000 3 10000 4 2066 0 16000 3
now have tried using query find of '0', problem i'm facing first row returns 4 instead of 3 searching whole row , not trailing. i've tried using find occurrence count
declare @longsequence int(max) declare @findsubstring int(max) set @longsequence = credit set @findsubstring = '0' select (len(@longsequence) - len(replace(@longsequence, @findsubstring, ''))) cntreplacedvals, (len(@longsequence) - len(replace(@longsequence, @findsubstring, '')))/len(@findsubstring) cntoccurancevals
is there way can find trailing 0's , not ones in middle of value?
edit: typo
you use reverse , patindex this:
declare @a table (i int) insert @a select 123000 union select 123001 union select 100000 union select 123001 select i, patindex('%[1-9]%',reverse(cast(i varchar(50)))) - 1 cntoccurancevals @a
Comments
Post a Comment