SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Addition Of Digits


Addition Of Digits

Author
Message
Mark Cowne
Mark Cowne
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11226 Visits: 26280
Just for fun...

Using a lookup table improves performance, a bit suprising (for me at least).


// Pre-calculated sums of 0 to 99
static readonly int[] TwoDigitSums = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
// Handle NULLs by adding RETURNS NULL ON NULL INPUT to wrapper if required
[Microsoft.SqlServer.Server.SqlFunction]
public static int SumDigits(int Input)
{
return (Input >= 0 ? TwoDigitSums[Input % 100] +
TwoDigitSums[(Input / 100) % 100] +
TwoDigitSums[(Input / 10000) % 100] +
TwoDigitSums[(Input / 1000000) % 100] +
TwoDigitSums[(Input / 100000000) % 100]
: TwoDigitSums[-(Input % 100)] +
TwoDigitSums[(Input / -100) % 100] +
TwoDigitSums[(Input / -10000) % 100] +
TwoDigitSums[(Input / -1000000) % 100] +
TwoDigitSums[(Input / -100000000) % 100]);
}



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Nadrek
Nadrek
SSCertifiable
SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)

Group: General Forum Members
Points: 7920 Visits: 2741
I know this is an old thread, but please note that Celko and Improved Celko are the only ones (of the non-CLR ones in the test harness, as written) that survive crossing into BIGINT values (i.e. over 2147483647 but less than 9223372036854775807).

Trivial extensions of some of the others results in errors, probably due to rounding, at least near the maximums.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search