Thread: Finding values in a column that are multiple of 4

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Finding values in a column that are multiple of 4

    I need to determine if the value in a in a DB2 database table column called cc (smallint with values ranging from 1 - 999) is a multiple of 4. If it is place a 'Y' in column mult4 (varchar (1)) else place a 'N' in this column. Anyone know how determine if the value is a multiple of 4? Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    n % 4 == 0 implies n is a multiple of 4.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Thank You

    Thanks for the help!

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You could do this with an EXEC SQL UPDATE statement. No need to write a program.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Thats what I am having problems doing..

    I am having problems using % in my update..set...where n % 4 == 0... could you elaborate how you would accomplish this? Thanks.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Sure. Send me a link to your SQL Reference manual for the platform you are using.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    115

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    WAG:
    Code:
    update my_table set mult4='Y' where mod(cc, 4) = 0;
    update my_table t set mult4='N' where mod(cc, 4) != 0;
    or maybe
    Code:
    update my_table set mult4 = case when mod (cc, 4) = 0 then 'Y' else 'N';
    That's from Googling stuff. I have no idea if it's right.
    Last edited by rags_to_riches; 04-17-2008 at 02:27 PM. Reason: Goofed

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I see that you have two choices.

    You can create an expression like
    Code:
    EXEC SQL UPDATE ... SET mult4 = 'Y' WHERE  ((cc/4)*4)=cc ;
    The above assumes your database manager uses integer division for smallints. You can research that.

    Or you can use mod()
    Code:
    EXEC SQL UPDATE ... SET mult4 = 'Y' WHERE mod(cc,4) = 0 ;
    That's my quick interpretation of the syntax.
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    That works great - Thanks

    It works. I was still trying to modulus operator for embedded sql. Thanks for your help.

    -Carl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. test to see if multiple values are the same
    By squeaker in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2006, 01:45 AM
  2. multiple values in #define(?) & counter placement
    By bnmwad in forum C Programming
    Replies: 2
    Last Post: 04-30-2006, 06:00 AM
  3. Returning Multiple Values from a function
    By Eddie K in forum C Programming
    Replies: 6
    Last Post: 03-12-2006, 01:18 PM
  4. Writing multiple values to an array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-06-2002, 03:27 PM
  5. c function returning multiple values
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-23-2001, 10:09 AM