June 30, 2016 at 8:24 am
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.
June 30, 2016 at 8:35 am
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.
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply