Sql Update with Calculation

Posted on 2011-04-26
Last Modified: 2012-05-11
I need to do an update statement where 3 columns are multiplied together and stored into another column.  I'm not quite sure how to accomplish this.

UPDATE tblRequestHistory SET strPrintCost = (SUM(CONVERT(numeric(8, 4), strPrintCost) * intPages * intCopies)) WHERE strPrintCost IS NOT NULL

Open in new window

intPages and intCopies are stored in the tblRequestHistory.  strPrintCost is also in the table and has a cost already assigned to it.  I need to multiply these 3 fields together and overwrite the sum to strPrintCost.  
Question by:gfedz
    LVL 15

    Accepted Solution

    What is the purpose of the SUM in your formula? Would you not be accomplishing the same thing if you removed it? Your query looks good, with the exception of the SUM().
    LVL 32

    Expert Comment


    UPDATE tblRequestHistory
    SET strPrintCost = cast(CONVERT(numeric(8, 4), strPrintCost) * intPages * intCopies as varchar)
    WHERE strPrintCost IS NOT NULL
    LVL 32

    Assisted Solution


    The result column should better be a numeric field instead of doing the conversion to string

    UPDATE tblRequestHistory
    SET strPrintCost = CONVERT(numeric(8, 4), strPrintCost) * intPages * intCopies
    WHERE strPrintCost IS NOT NULL
    LVL 1

    Author Closing Comment

    I'm still pretty new to syntax in sql.  I assumed it was something like this.  Thank you guys for your time.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

         When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
    Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video discusses moving either the default database or any database to a new volume.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now