Author |
Topic |
davidc
Starting Member
26 Posts |
Posted - 2007-05-09 : 06:51:33
|
I need to take a numeric value and add together the cubes of each single digit. The string length may vary from row to row.As as example the number 1234 has individual cube values of 1 (1 cubed), 8 (2 cubed), 27 (3 cubed) and 64 (4 cubed). The resulting sum of the cubes is then 1+8+27+64 = 100. The same result would occur for 1243.The purpose is to look for numeric values in a table where the same digits occur in two different rows but in a different order.How can I create a function to apply to a varchar column to produce my integer result ? |
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-05-09 : 08:29:37
|
Convert each character to ascii and take the cube as u mentionedlike Power(Ascii(substring(col1,1,1)),3)one doubt, even sum also will give the correct value right.1234 - 1+2+3+4 = 101324 - 1+3+2+4 = 10why u want cube of each digit and sum??????? |
|
|
davidc
Starting Member
26 Posts |
Posted - 2007-05-09 : 09:15:45
|
I am trying to create a function which can be applied to the numbers in a column. The data in this column contains invoice numbers input by hand by an input clerk. They sometimes get the numbers in the wrong order (e.g. 1243 instead of 1234). If I cube all the digits first then add them together I get a number which is the same for both rows. I can then look for invoices which have the same digits in a different order to check that they have not been input twice in error.E.G the value is entered as 1234 the first time. Then a second person accidentally enters the same invoice but with an error in the number - 1243. The function will calculate the same sum of cube for these two entries. The problem is the invoice number can have any number of digits so I need to start with digit one, and then digit two, then up to the last digit. |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-09 : 09:18:25
|
What if there are actually two different invoices with their numbers differing in just a single place like 1234 and 1243 ?Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
|
|
davidc
Starting Member
26 Posts |
Posted - 2007-05-09 : 09:26:12
|
Not a problem - I only want to know if there are transposed invoice numbers. Other data in the table will confirm if they are transposed or not. I am more interested in finding someone who can help me with the function i need |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-05-09 : 09:43:05
|
you are better off with an algorithm that sorts the digits.i.e., something that returns:472912-->122479742912-->122479472921-->122479this way you can verify for sure that if the formula returns the same value but the original entries do not match, a transposition has most likely occurred. Cubing the digits will not help you determine this. What do you think?- Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-09 : 09:54:32
|
Seems like you need a function to implement Hamming distance algorithm.[url]http://en.wikipedia.org/wiki/Hamming_distance[/url]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
X002548
Not Just a Number
15586 Posts |
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2007-05-09 : 10:48:17
|
So how do you know if it's transposed or if it's they way it's suppose to be?What kind of equality check is that?It either is or it isn'tNow if you're talking about lookig for possible canidates for error, then I'll go with you there, but it still comes down to manually lookig at stuff, and still eliminates the full blown mistakes...Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-09 : 10:48:36
|
Oh..Jeff, You beat me there!! I was planning to write the function myself.Why don't you move the function to the script library?Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-09 : 13:02:22
|
Seems like a simplified method of Levenstiens distance algorithm.This calculates the cube sumdeclare @i intset @i = 1234select sum(power(@i / power(10, d.number) % 10, 3)) as cubesumfrom ( select number from master..spt_values where number between 0 and 9 and name is null ) as d Peter LarssonHelsingborg, Sweden |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-05-10 : 06:12:17
|
Peso. You posted what I kept in mindAlternatively you can squre the sum of digits alsoselect square(sum(@i / power(10, d.number) % 10)) as cubesumfrom ( select number from master..spt_values where number between 0 and 9 and name is null ) as d MadhivananFailing to plan is Planning to fail |
|
|
|