Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Comparing an integer value against other integers

Author  Topic 

al_gore_rythem
Starting Member

1 Post

Posted - 2013-03-29 : 10:26:34
I would like to take an integer field and compare the values (record by record) to see if the SUM OF THE INTEGER FIELDS are the same and make sure the integers in the field match. Here is an example

520
502
205
052

Same digits and if u add the digits up they all equal 7.
What is the best way to achieve this analysis

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2013-03-29 : 11:14:49
Those aren't integers, they're strings. This will work for strings of length 3, you'll have to adapt it if you have string of length > 3
DECLARE @Table Table (Col1 Char(3))
INSERT INTO @Table
VALUES ('520'),('502'),('205'),('052')

select col1,CONVERT(int,Col1)/100 + CONVERT(int,Col1)/10 % 10 + CONVERT(int,Col1) % 10
from @table

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -