Change column from varchar to int?

Posted on 2005-02-25
Medium Priority
Last Modified: 2008-02-01
I have a table in a database that has a column defined as varchar 12.  I attempted to use the sum function in isql and it failed.  

Server: Msg 409, Level 16, State 2, Line 2
The sum or average aggregate operation cannot take a varchar data type as an argument.

Does this have to be INT or something else?  Is it possible to change the column now that it has been created and populated?  It only has numbers in it.  How can I go about getting around this?

Question by:robrandon
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 23

Accepted Solution

adathelad earned 500 total points
ID: 13404542
SUM will only be able to work on numerical data such as INT.

If all the values in the column are integers, then yes you can easily change it. You should just be able to go into design view in Enterprise Manager and change it from a VARCHAR to INT.

If if you want to keep them as varchar, try:
 but really, if they will always hold numerical data it should be an appropriate datatype

Assisted Solution

john_reddin earned 500 total points
ID: 13404640
   A mathamatical function can only be performed on a numeric field.   You could either change the data type or use a Convert/Cast function.

The convert syntax is convert(int, <column_name>)  
   - replace the angle brackets and column_name with the correct value
   - This works only if the values within the column remain numeric.

To convert the data type to an int then you can do the following.

Alter table <table_name> alter column <column_name> int

   -This is a more perminant solution, and assumes the values within
    <column_name> are  between -2^31 (-2,147,483,648) and
     2^31 - 1 (2,147,483,647).

LVL 16

Author Comment

ID: 13404860
Awesome.  Thanks!

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question