How do I do this?

  • UPDATE Table

        SET   qty_total = start_qty - end_qty

     

    But, if qty_total ends up being negative, I want to update with a 0.

  • You can use a CASE expression for this:

    UPDATE Table

    SET qty_total = CASE WHEN (start_qty - end_qty) < 0 THEN 0 ELSE (start_qty - end_qty) END

    --
    Adam Machanic
    whoisactive

  • What about

    UPDATE Table

        SET   qty_total =

         case when start_qty - end_qty < 0 then 0 else start_qty - end_qty end

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Damn, beaten by 2 minutes, Adam!

    Should take a course in fast typing

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • No worries, you've got me beat by 3300 posts already ... Too bad there's not a prize

    --
    Adam Machanic
    whoisactive

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply