ASPForums.Net RSS Feed additions to the content that appears on ASPForums.Net(c) 2019 All rights reserved.Calculate Sum of all and Odd Characters in a String in SQL Server<p>Hi&nbsp;Mehram,</p> <p>Check the below sample query. Use while loop to iterate each character and count the Total and the odd total.</p> <p><strong><span style="text-decoration: underline;">SQL</span></strong></p> <pre class="brush: sql">DECLARE @test VARCHAR(20),@Counter INT,@Sum INT,@OddSum INT SET @test = &#39;0100501&#39; SET @Counter = 1 SET @Sum = 0 SET @OddSum = 0 WHILE (@Counter &lt;= LEN(@test)) BEGIN SET @Sum = @Sum + SUBSTRING(@test,@Counter,1) IF(@Counter % 2 != 0) BEGIN SET @OddSum = @OddSum + SUBSTRING(@test,@Counter,1) END SET @Counter = @Counter + 1 END SELECT @Sum &#39;Total Sum&#39; SELECT @OddSum &#39;Odd sum&#39;</pre> <p><strong><span style="text-decoration: underline;">Output</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="" alt="" width="246" height="152" /></span></strong></p>, 02 Jan 2019 04:22:48 GMT