I need help to write a SQL query that will replace values in one column by another column values in the same table. For example, given the following table, I want to replace values in column 2 by values in column 1. I think the "UPDATE table SET ..." clause would help but don't know how to use it. Can any one help me, please ?
enter image description here
UPDATE table t1
SET column1 = (SELECT column2 FROM table t2 WHERE t1.column1 = t2.column1);
I have table 1.
I have another empty table 2 with the following columns.
I want to insert into table 2 by selecting from table 1 - so I write the query as:
insert into table2(employee,id,zone,url)
select employee, id, zone, concat('https://',employee,'.com/',id,'?',zone)
from table1
Now my table 2 looks like this,
Now for the authcode column, I do the following and insert it into the table2.
insert into table2(authcode)
SELECT CONVERT(VARCHAR(10),HASHBYTES('MD5', substring(URL,8,100)),2)
from table2.
But the insert happens differently like this AS AN ENTIRE NEW SET OF ROWS.
Can someone help me to insert the last column to the corresponding rows instead of it creating a new one?
What you should be doing is UPDATE the table to fill the column authcode, but you could do it all in 1 step while you are inserting the rows:
insert into table2(employee,id,zone,url, authcode)
select
employee,
id,
zone,
concat('https://',employee,'.com/',id,'?',zone),
CONVERT(VARCHAR(10),HASHBYTES('MD5', substring(concat('https://',employee,'.com/',id,'?',zone),8,100)),2)
from table1
or if you want to update:
update table2
set authcode = CONVERT(VARCHAR(10),HASHBYTES('MD5', substring(URL,8,100)),2)
where authcode is null
The result you are seeing is the intended behavior for an INSERT statement. It will always insert new rows.
If you want to modify existing rows your need to use an UPDATE statement.
You can either modify your INSERT to look like what #forpas has posted to get all this work done in one step. Another option is to modify the second INSERT to be an UPDATE like the following:
update table2
set authcode = CONVERT(VARCHAR(10),HASHBYTES('MD5', substring(URL,8,100)),2)
I don't know what I am doing. Extra new at this. Below I am trying to make corrections to the data on a column, with out losing any of the data, just overriding it. In this Column, some of the cells have characters spaces (spacebar) in them so it dose not show up as "NULL".
In my fist attempt, I can see the query data and it looks good, 100% correct. But don't know how to put that data into the table I got it from. So I need to replace the data in column 'Speedlink_IP' with my Queried results.
Thanks every one in advance!
1st Attempt -
SELECT NULLIF(LTRIM(RTRIM(Speedlink_IP)), '')
As Speedlink_IP
FROM Master_IP_Data
INSERT INTO TEMP1 (col1)
2nd attempt -
CREATE TABLE TEMP1 (
col1 varchar (50) NULL
);
SELECT NULLIF(LTRIM(RTRIM(Speedlink_IP)), '')
As Speedlink_IP
FROM Master_IP_Data
INSERT INTO TEMP1 (col1)
INSERT INTO dbo.Master_IP_Data (Speedlink_IP)
SELECT col1
FROM TEMP1
;
DROP Table TEMP1
You seem to be looking for a simple UPDATE statement.
UPDATE Master_IP_Data
SET Speedlink_IP = NULL
WHERE LTRIM(RTRIM(Speedlink_IP)) = ''
This query will turn to NULL values of Speedlink_IP that contains only spaces. You don't need to use a temporary table for this.
I have a scenario, where 100's of select statements sql's are in one metadata table or some text file.
Need to insert all sql results into one specific table. (master table has col1, col2,col3 .... 200columns )
problem im facing(ORA-00947) is every select statement has different number of columns.
.. i need to generate INSERT PART.
CASE 1 : INSERT INTO (COL1,COL2,COL3) <<this select part comes from file/variable>>
CASE 2 : INSERT INTO (COL1) <<this select part comes from file/variable>>
CASE 3 : INSERT INTO (COL1) <<this select part comes from file/variable>>
have to figure out how many columns are in select part then generate INSERT part.
.
Thought of create as select but problem is some select statement has max(col) without alias so it will fail.
This is too long for a comment.
If you are storing SQL in a table, then you are constructing your query dynamically. So, update the table and list the columns that you want.
You could then construct the inserts as :
insert into master_table (<column list here>)
<select here>;
Both the select and column list would come from the table.
By far the easiest is to create a view for each SELECT statement. Then you can query the USER_TAB_COLUMNS view on the view name and get the column names.
Best regards,
Stew Ashton
I tried to insert some data from one table (tableA)
into another table (tableB) using bulk insert but cannot succeeded because the tableA data has got one value-> BERMUDA 23''-24''
when it tries to enter this value it raise error as
String or binary data would be truncated.
My query is
insert into tableA
select size from tableB
i try with replace the single quote with empty space but as its size we should enter the data with single quote only.
Please suggest the way out.
you can remove single quote with replace function
insert into tableA
select replace(size,'''','') from tableB