How to add individual number in a Cell using sql

Posted by Kirthiga under Sql Server on 5/22/2018 | Points: 10 | Views : 2046 | Status : [Member] | Replies : 1
In my table MyTable I have a column which contains 8 digit numeric value.

(ie)
Data
20151201
20151204
20181208
20181219

My requirement is I need a new column named Sum and the data should be sum of MyColumn values with below formula.

Data SumColumn
20151201 3
20151204 6
20181208 4
20181219 6

Formula is

For Example
20181219

(2+0+1+8) + (1+2) + (1+9)

(11) + (3) + (10)

(1+1) + (3) + (1+0)

(2) + (3) +(1)

(6)




Responses

Posted by: Rajnilari2015 on: 5/23/2018 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Try this

declare @t table(Data  int)
insert into @t select 20151201 union all select 20151204 union all select 20181208 union all select 20181219

select Data , SumColumn=(Data - 1) % 9 + 1
from @t


Result
-----------
Data SumColumn
20151201 3
20151204 6
20181208 4
20181219 6



--
Thanks & Regards,
RNA Team

Kirthiga, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response