How to update a field with the same Value within a Range using SQL?

  • Bit new to SQL,

    I have a table with multiple fields, the AccNumber field begin 1 and go up to 1000

    In each Row, there is a field called Expiry Date (these are currently all different dates)

    I want to pick a range of AccNumber (from 1-500) and update the Expiry Date to 30/06/2016

    Can anyone show me how to do this?

    I have tried sql queriy in management studio and have managed to list the range using the below

    from mytable

    where AccNumber between '1' and '500'

    But I'm not sure how to update the Expiry date filed for this range to be set to 30/06/2016

    Any help would be appreciated.

  • It's easy to convert a SELECT into an UPDATE.

    You have 2 options depending on the complexity of the UPDATE. You can keep the FROM and use a table alias in the UPDATE.

    UPDATE t SET

    ExpiryDate = '20160630'

    from mytable t

    where AccNumber between 1 and 500;

    Or you could remove the FROM clause

    UPDATE mytable SET

    ExpiryDate = '20160630'

    where AccNumber between 1 and 500;

    Note that:

    - I added a semicolon which is optional but might not be that way in the future (it isn't for certain statements).

    - I removed the quotes to handle the values as integers.

    - I'm using the ISO format for dates which is not dependent on any settings, unlike most other formats.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 2 posts - 1 through 1 (of 1 total)

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