Whah
asked on
SQL replace or update for multiple values in a column
Hi,
I am having a challenge where I need to update multiple values in a column of a SQL database where I need to add a code to the existing value.
Example:
Now:
Apples
Oranges
Bananas
Grapes
I need to change it to:
Apples - 1234
Oranges - 2345
Bananas - 3456
Grapes - 4567
Any help is appreciated.
Thanks,
Whah
I am having a challenge where I need to update multiple values in a column of a SQL database where I need to add a code to the existing value.
Example:
Now:
Apples
Oranges
Bananas
Grapes
I need to change it to:
Apples - 1234
Oranges - 2345
Bananas - 3456
Grapes - 4567
Any help is appreciated.
Thanks,
Whah
How do you determine the code? Do you have the code in any other column?
ASKER
Hi Sharath_123,
No, the code isn't in any existing columns. I have been given an excel spread sheet with all of the old values and their corresponding new values that now have a code associated with each one.
Thanks,
Brian
No, the code isn't in any existing columns. I have been given an excel spread sheet with all of the old values and their corresponding new values that now have a code associated with each one.
Thanks,
Brian
insert the data from the xls into a temp table and then update the data in actual table.
does this help?
CREATE TABLE NewCodes
([GivenString] varchar(7), [WantedCode] int)
;
INSERT INTO NewCodes
([GivenString], [WantedCode])
VALUES
('Apples', 1234),
('Oranges', 2345),
('Bananas', 3456),
('Grapes', 4567)
;
CREATE TABLE FromExcel
([GivenString] varchar(7), [WantedCode] int)
;
INSERT INTO FromExcel
([GivenString], [WantedCode])
VALUES
('Apples', NULL),
('Oranges', NULL),
('Bananas', NULL),
('Grapes', NULL)
;
update FromExcel
set FromExcel.WantedCode = NewCodes.WantedCode
from FromExcel
inner join NewCodes ON FromExcel.GivenString = NewCodes.GivenString
;
**Query 1**:
select
*
from FromExcel
**[Results][2]**:
| GIVENSTRING | WANTEDCODE |
|-------------|------------|
| Apples | 1234 |
| Oranges | 2345 |
| Bananas | 3456 |
| Grapes | 4567 |
[1]: http://sqlfiddle.com/#!3/d733b/1
ASKER
Thank you very much for the help. I will give this a try today, and let you know how it goes. Thanks again.
ASKER
Thank you for the suggestions so far, but I've gotten some more information on what really needs to be done.
TableA
Column1
Apples
Oranges
Bananas
Grapes
I have taken the Excel file and imported into another SQL table. What I need now is if TableA's Column1 matches TableB's Column1 then take the data from TableB Column2, and put it in a new column called Column2 in TableA. They want to retain the data as it appears in TableA Column1, and translate the new value that is in this other temp table with the new values, and add a new column with what the new value should be.
I need this:
TableA
Column1 Column2
Apples 01-Apples
Oranges 02-Oranges
Bananas 03-Bananas
Grapes 04-Grapes
Thank you very much for your help.
TableA
Column1
Apples
Oranges
Bananas
Grapes
I have taken the Excel file and imported into another SQL table. What I need now is if TableA's Column1 matches TableB's Column1 then take the data from TableB Column2, and put it in a new column called Column2 in TableA. They want to retain the data as it appears in TableA Column1, and translate the new value that is in this other temp table with the new values, and add a new column with what the new value should be.
I need this:
TableA
Column1 Column2
Apples 01-Apples
Oranges 02-Oranges
Bananas 03-Bananas
Grapes 04-Grapes
Thank you very much for your help.
ASKER
I may have simplified this too much in my example because really there are many instances of the different fruit in column1, and all need to have the corresponding new value in the new column. Just wanted to add this in case it matters. Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Sharath,
This is mine based from your example.
UPDATE HACSB_Test
SET HACSB_Test.PROPERTY = CONVERT(NVARCHAR(50),TEMP. PROPERTY)+ '-'+HACSB_ Test.PROGR AM
FROM HACSB_Test
JOIN TEMP
ON HACSB_Test.PROGRAM = TEMP.PROPERTY
I am getting 0 rows affected.
Just to further clarify, I need: If HACSB_Test.Program=(Whatev er the match is in Temp.Program) then copy corresponding value from the Temp.PROPERTY to HACSB_Test.Property.
Both Program and Property exist in the "Temp" table with Temp.Program being the match of the old value in HACSB_Test.Program, and Property being the new value that only exists right now in Temp.Property that needs to get copied to the corresponding HACSB_Test.Property. I have already created the Property field in the HACSB_Test table.
Hope this helps, and thank you again for the help.
This is mine based from your example.
UPDATE HACSB_Test
SET HACSB_Test.PROPERTY = CONVERT(NVARCHAR(50),TEMP.
FROM HACSB_Test
JOIN TEMP
ON HACSB_Test.PROGRAM = TEMP.PROPERTY
I am getting 0 rows affected.
Just to further clarify, I need: If HACSB_Test.Program=(Whatev
Both Program and Property exist in the "Temp" table with Temp.Program being the match of the old value in HACSB_Test.Program, and Property being the new value that only exists right now in Temp.Property that needs to get copied to the corresponding HACSB_Test.Property. I have already created the Property field in the HACSB_Test table.
Hope this helps, and thank you again for the help.
ASKER
Thanks, Sharah. I really appreciate your help.