0

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.

Example:

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

JeremyK
  • 1,075
  • 1
  • 22
  • 45

4 Answers4

1

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'
Matt Klinker
  • 773
  • 5
  • 18
1
UPDATE  T
SET     T.END_DATE = DATEADD(YEAR,1,T.BEGINDATE)
FROM    TABLE T
WHERE   T.DESCRIPTION = 'value_i_need
A.J
  • 382
  • 2
  • 6
1

Do you mean:

UPDATE [TABLE] SET ENDDATE=DATEADD(YEAR,1,BEGINDATE) WHERE DESCRIPTION='VALUE_I_NEED'
Punter015
  • 1,718
  • 10
  • 13
1

Try like this

UPDATE
    Table T1
SET
    T1.endDate=DATEADD(year, 1, beginDate)

WHERE T1.description='value_i_need'
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115