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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

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

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

Starting Member

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


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

Flowing Fount of Yak Knowledge

2875 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))
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  
 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.04 seconds. Powered By: Snitz Forums 2000