SQL Server - Select list and update a field in each row - sql

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

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'

Related

How to show specific value in a column - sql (redshift)

I have a situation where I want to show a value that corresponds to one field in a table but in all rows. What is better is to show you an example in a screenshot:
What I want is to have the value of 3807 in every row? How can I do that?
THank you in advance.
Try using MAX as an analytic function:
SELECT
site_id,
bt_max_speed,
MAX(bt_max_speed_on_site_coverage) OVER (PARTITION BY site_id) bt_max_speed_on_site_coverage
FROM yourTable
ORDER BY site_id;
If you simply want to update one column so it has same value for all rows you can just use UPDATE without any conditions
UPDATE <table_name> SET <column_name> = <value>
In your case it would look like this, but with replaced with actual name of your table
UPDATE <table_name> SET bt_max_speed_on_site_coverage = 3807

postgresql: Fast way to update the latest inserted row

What is the best way to modify the latest added row without using a temporary table.
E.g. the table structure is
id | text | date
My current approach would be an insert with the postgresql specific command "returning id" so that I can update the table afterwards with
update myTable set date='2013-11-11' where id = lastRow
However I have the feeling that postgresql is not simply using the last row but is iterating through millions of entries until "id = lastRow" is found. How can i directly access the last added row?
update myTable date='2013-11-11' where id IN(
SELECT max(id) FROM myTable
)
Just to add to mvb13's answer (since I don't have enough points to comment directly yet) there is one word missing. Hopefully, this will save someone some time from working out the correct syntax LOL.
update myTable set date='2013-11-11' where id IN(
SELECT max(id) FROM myTable
);

CONCAT in Oracle

I've been researching the concat function extensively but hit a wall creating a temp table. I have two columns: ID (ex. 4323) and Source (ex. PHI). I want to add a column that includes a prefix of "API-" to the ID column (ex. API-4332). Anyone have any insight?
All rows?
UPDATE TheTable
SET id = 'API-'||id;
Or,
UPDATE TheTable
SET id = CONCAT('API-',id);
EDIT:
Your problem statement led me to believe you wanted to create a new column ("I want to add a column..."). Sorry for the confusion. I have changed the answer to update the ID column.

Convert value in all rows to VARBINARY(50)

I want to upgrade all columns in a table, what I am aiming to do is retreive a column from the row it will update then update it, something like:
update works
set encrpyted_item_no = (CAST(RTrim((
select unencrypted_item_no
from works
where name = name
) AS VARBINARY(50))
I know that query is wrong, it's just an example to show you what I am aiming to do.
I want it to select the column unencrypted_item_no from its row then update that same row with the data it gets from unencrypted_item_no, doing this for the whole table.
How would I accomplish this?
You shouldn't need to do a sub-select, referencing the other column in the set will work on a row by row basis, ie:
UPDATE works
SET encrpyted_item_no = CAST(RTrim(unencrypted_item_no) AS varbinary(50))
Shouldn't this be enough ?
update works
set encrypted_item_no = CAST(RTrim(unencrypted_item_no) AS VARBINARY(50))

SQL SELECT or INSERT INTO query

I'm working with SQL Server 2000. I need to take the results from one column (VALIMIT) and insert them into another column (VALIMIT2012) in the same table (lending_limits).
My question is do I need to do a SELECT query first, or do I just start with an INSERT INTO query and what the proper syntax would be for the INSERT INTO query.
You can do this with an UPDATE statement:
update lending_limits
set VALIMIT2012 = VALIMIT
Neither. You don't insert columns, you insert rows, so what you want is an update:
update SomeTable
set VALIMIT2012 = VALIMIT
Note: It looks like you have one column per year, which is bad database design. If you have different data for each year, you should put that in a separate table, so that you get the year as data, not part of the column name.
UPDATE TableName SET VALIMIT2012 = VALIMIT