SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Comparing an integer value against other integers
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

al_gore_rythem
Starting Member

USA
1 Posts

Posted - 03/29/2013 :  10:26:34  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 03/29/2013 :  11:14:49  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.86 seconds. Powered By: Snitz Forums 2000