SQL Server - Select list and update a field in each row
I hate to ask something that I know has been answered, but after 45 minutes of searching I yield to everyone here.
I have a table where I need to select a list of items based on a value, and then take one field and add a year to it and update another column with that value.
select * from table where description='value_i_need' UPDATE table SET endDate=DATEADD(year, 1, beginDate) -- For each item in the select
EDIT: The select statement will return a ton of results. I need to update each row uniquely.
I don't know how to combine the select or loop through the results.
My SQL knowledge is limited, and I wish I could have figured this out without having to ask. I appreciate any help
Edit 2: I have one table that I need to go through and update the end date on each item that matches a description. So I have 1000 "Computers" for example, that I need to update a field in each row with that description based on another field in the same row. All coming from one table
Try like this
UPDATE Table T1 SET T1.endDate=DATEADD(year, 1, beginDate) WHERE T1.description='value_i_need'
Unless I missed something, you should be able to simply add your criteria to your update statement as follows:
UPDATE [table] SET endDate=DATEADD(year, 1, beginDate) WHERE description='value_i_need'
UPDATE T SET T.END_DATE = DATEADD(YEAR,1,T.BEGINDATE) FROM TABLE T WHERE T.DESCRIPTION = 'value_i_need
Do you mean:
UPDATE [TABLE] SET ENDDATE=DATEADD(YEAR,1,BEGINDATE) WHERE DESCRIPTION='VALUE_I_NEED'