Updating multiple columns with several conditions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pratsadhu
    New Member
    • Mar 2007
    • 16

    Updating multiple columns with several conditions

    Hi,

    I have a table which has columns name, date1, date2.
    Through 'Or' condition, i can update name but to update date1 and date2 I have another condition. ID1 matches, date1 will be updated and date2 on ID2.
    How to set these columns in a single query?


    Thanks,
    Prat
  • deepuv04
    Recognized Expert New Member
    • Nov 2007
    • 227

    #2
    Originally posted by pratsadhu
    Hi,

    I have a table which has columns name, date1, date2.
    Through 'Or' condition, i can update name but to update date1 and date2 I have another condition. ID1 matches, date1 will be updated and date2 on ID2.
    How to set these columns in a single query?


    Thanks,
    Prat
    hi,
    try something like
    [code=sql]
    UPDATE Table_Name
    SET Name = @New_Name,
    Date1 = CASE @Value WHEN ID1 THEN @New_Date
    ELSE Date1 END,
    Date2 = CASE @Value WHEN ID2 THEN @New_Date
    ELSE Date2 END
    WHERE Your_Condition
    [/code]
    thanks

    Comment

    • pratsadhu
      New Member
      • Mar 2007
      • 16

      #3
      Thanks a lot! It works



      Originally posted by deepuv04
      hi,
      try something like
      [code=sql]
      UPDATE Table_Name
      SET Name = @New_Name,
      Date1 = CASE @Value WHEN ID1 THEN @New_Date
      ELSE Date1 END,
      Date2 = CASE @Value WHEN ID2 THEN @New_Date
      ELSE Date2 END
      WHERE Your_Condition
      [/code]
      thanks

      Comment

      Working...