473,480 Members | 2,325 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I calculate the sum of each number in a string of numbers?

I've got a string of numbers, say 123456 (the actually number is 12 digits
long). I need to calculate the sum of each individual number in the string
of numbers, so in the example of 123456 the sum would be 21 (1+2+3+4+5+6).

Thanks
Jul 20 '05 #1
5 12936
What about something like

CREATE FUNCTION dbo.f_ReturnSum (@inputVal varchar(12))
RETURNS INT
AS
BEGIN
declare @i TINYINT
declare @returnedVal int
SET @i = 1
SET @returnedVal = 0
WHILE @i <= LEN(@inputVal)
Begin
set @returnedVal = @ReturnedVal + CAST(SUBSTRING(@inputval,@i,1) as
tinyint)
set @i = @i + 1
End
RETURN @returnedVal
END
GO

select dbo.f_ReturnSum('125111111111')

--
--

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.SQLDTS.com - The site for all your DTS needs.
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org
"Chris Michael" <ne**@intomobiles.com> wrote in message
news:c6************@ID-211081.news.uni-berlin.de...
I've got a string of numbers, say 123456 (the actually number is 12 digits
long). I need to calculate the sum of each individual number in the string
of numbers, so in the example of 123456 the sum would be 21 (1+2+3+4+5+6).

Thanks

Jul 20 '05 #2
"Chris Michael" <ne**@intomobiles.com> wrote in message
news:c6************@ID-211081.news.uni-berlin.de...
I've got a string of numbers, say 123456 (the actually number is 12 digits
long). I need to calculate the sum of each individual number in the string
of numbers, so in the example of 123456 the sum would be 21 (1+2+3+4+5+6).

Thanks


Needless to say, this isn't the right representation of digits for summation.
However, try

CREATE TABLE T
(
digits CHAR(12) NOT NULL PRIMARY KEY
)

INSERT INTO T (digits)
VALUES ('123456789123')
INSERT INTO T (digits)
VALUES ('555555555555')

SELECT T.digits,
SUM(CAST(SUBSTRING(T.digits, n, 1) AS INT)) AS digits_sum
FROM (SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL
SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL
SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL
SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12) AS
I(n)
CROSS JOIN
T
GROUP BY T.digits

digits digits_sum
123456789123 51
555555555555 60

--
JAG
Jul 20 '05 #3
"Chris Michael" <ne**@intomobiles.com> wrote in message
news:c6************@ID-211081.news.uni-berlin.de
|| I've got a string of numbers, say 123456 (the actually number is 12
|| digits long). I need to calculate the sum of each individual number
|| in the string of numbers, so in the example of 123456 the sum would
|| be 21 (1+2+3+4+5+6).

Thanks a lot Allan and John. Exactly what I needed.

--
Chris Michael
www.INTOmobiles.com
Free ringtones/logos
Free mobile alerts
3 months free insurance
Jul 20 '05 #4
John Gilson (ja*@acm.org) writes:
Needless to say, this isn't the right representation of digits for
summation.


I don't know what Chris is up to, but I needed to do this some days
ago. My task was to complete the number with a check digit, and to
do this you need the sum of the digits, each digit multiplied with
a certain weight.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5
>> I need to calculate the sum of each individual number in the string
of numbers, so in the example of 123456 the sum would be 21
(1+2+3+4+5+6). <<

Can I assume this is for a check digit calculation? The best solution
is to store the number as a string and use a summation of CAST()
expressions, to validate the final check digit:

CREATE TABLE Foobar
(..
i CHAR(12) NOT NULL
CHECK (i LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]',
CHECK ((CAST(SUBSTRING(i,1,1) AS INTEGER) + CAST(SUBSTRING(i,2,1) AS
INTEGER) + ..)%10 = CAST(SUBSTRING(i,12,1) AS INTEGER),

);
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1171
by: Chris Michael | last post by:
I've got a string of numbers, say 123456 (the actually number is 12 digits long). I need to calculate the sum of each individual number in the string of numbers, so in the example of 123456 the sum...
1
9967
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a...
6
7276
by: jochen scheire | last post by:
Is there a way I can calculate a field in a form based on another field in the same form. When clicking submit, both values should be posted to the next page. I want to be able to type in a value...
4
1803
by: Sam Kong | last post by:
Hello! I wonder if there's a good way to make a function that calculates 2 big numbers (whole numbers) expressed as strings and return a result string with the value of a number? For example:...
9
2711
by: Lyners | last post by:
Quick question. I have some java script that looks like this; ...
5
2213
by: milan.letic | last post by:
Hello, I believe that this has been answered somewhere but I cannot find it. I have 51 textboxes on page. In 50 I can enter value. 51st should tell me what is average of entered numbers. As...
7
25964
by: Sam | last post by:
Hi, I use C# in my ASP.NET projects. Here's what I need to do: I want to add x business days to a given date i.e. add 12 business days to today's date. What is the best, fastest and most...
5
3572
by: kebabkongen | last post by:
Hi, I have an XML source which gives me the start time (in the format hh:mi) of a program and the duration of the program (in minutes). With XSLT only, I would like to generate the time the next...
6
5642
by: moshiur | last post by:
I'm a very new programmer. I have a sorted file and want to calculate delats between the numbers. That is calculate the difference between first number in line 1 and second number in line 2...
5
24519
FishVal
by: FishVal | last post by:
IMHO, the following is not a how-to-do instruction to solve a particular problem but more a concept-proof stuff demonstrating possibilities of SQL. So, let us say the problem is to calculate...
0
7051
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7097
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4493
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3003
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.