Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP 3 Classic ASP Active Server Pages 3.0 > Classic ASP Basics
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 28th, 2004, 06:49 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 198
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via MSN to itHighway
Default Calculating "Sum" of Numeric values in Varchar fld

Hello!

I have a Varchar field, in which has only numeric values. I want to calculate the sum of using the sql query.
Right now the query I"m using is:
SSQL = "select SUM(AMOUNT) AS VARAMOUNT from ORDER"

The above sql statement gives the following error:
Cannot calculate SUM of varchar field

Any idea..
Any help...
Will be appriciated....

Thanks
Zeeshan Ahmed

 
Old December 28th, 2004, 09:13 PM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 363
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,

  The varchar field in the sence it contains only numbers or alphanumeric characters? If numbers only, convert to any of numeric datatype.


------------
Rajani



 
Old December 29th, 2004, 12:52 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

itHighway
Your error explains it clearly:
;;;Cannot calculate SUM of varchar field
means what it says. Does this field need to be a varchar? varchars are stored as strings. Two options come to mind:
1..re think you data base design changing this field to some type of integer
2..Get the values, convert to integer...

Wind is your friend
Matt
 
Old December 29th, 2004, 05:16 AM
Registered User
 
Join Date: Dec 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi ithieghway

If your field contain only number then try this code

select sum(to_number(AMOUNT)) AS VARAMOUNT from ORDER

let me know how it works

 
Old December 29th, 2004, 09:34 AM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

I tested this in SQL Server 2000:

I have an Varchar column by name "age". The below query would SUM the column value and display.

Select Sum(Convert(int, age)) From CustomerData

Best Regards
Vadivel

MVP ASP/ASP.NET
http://vadivel.thinkingms.com
 
Old December 29th, 2004, 06:55 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 198
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via MSN to itHighway
Default

Hi Vadivel,

It worked,
thanks.. thank a lot

Khush Rahoo

 
Old December 29th, 2004, 07:41 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

itHighway

What if someone types 'one' or 'two' etc into this field? your sum result could be incorrect. There are more reasons why a field that is to be summed should be some sort of integer. Anyway, glad you sorted it.




Wind is your friend
Matt





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to insert varchar data in numeric column dhirajDac Classic ASP Components 3 March 31st, 2008 06:23 AM
Calculating Gridview Column Values rit01 ASP.NET 2.0 Professional 1 October 18th, 2006 03:46 PM
Calculating input values with arrays Ray Z Javascript 0 July 19th, 2006 09:20 PM
calculating values aceconcepts PHP How-To 0 February 16th, 2006 09:01 AM
Calculating sum of attributes Kitty XSLT 3 June 12th, 2005 05:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.