To insert single quote values into the table using "insert into" - sql

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

Related

How to append unique values from temp_tbl into original_tbl (SQL Server)?

I have a table that I'm trying to append unique values to. Every month I get list of user logins to import into this table. I would like to keep all the original values and just append the new and unique values onto the existing table. Both the table and the flatfile have a single column, with unique values, built like this:
_____
login
abcde001
abcde002
...
_____
I'm bulk ingesting the flat file into a temp table, with this:
IF OBJECT_ID('tempdb..#FLAT_FILE_TBL') IS NOT NULL
DROP TABLE #FLAT_FILE_TBL
CREATE TABLE #FLAT_FILE_TBL
(
ntlogin2 nvarchar(15)
)
BULK INSERT #FLAT_FILE_TBL
FROM 'C:\ImportFiles\logins_Dec2021.csv'
WITH (FIELDTERMINATOR = ' ');
Is there a join that would give me the table with existing values + new unique values appended? I'd rather not hard code a loop to evaluate it line by line.
Something like (pseudocode):
append unique {login} from temp_tbl into original_tbl
Hopefully it's an easy answer for someone out there.
Thanks!
Poster on Reddit r/sql provided this answer, which I'm pursuing:
Merge statement?
It looks like using a merge statement will do exactly what I want. Thanks for those who already posted replies.
You can check if a record exists using 'EXISTS' clause and insert if it doesn't exist in the target table. You can also use MERGE statement to achieve the same. Depending on what you want to do to the existing records in the target table, you can modify the Merge statement. Here since you only want to insert new records, you need to specify only what you want to do when a new record comes in. Here is an example
MERGE original_tbl T
USING temp_tbl S
ON T.login = S.login
WHEN NOT MATCHED THEN
INSERT (login)
VALUES(S.login)
Another solution would be to left join the target table to the temp table and insert only when the record doesn't exist.
INSERT INTO original_tbl(login)
SELECT S.Login
FROM temp_tbl S
LEFT JOIN original_tbl T
ON S.Login = T.Login
WHERE T.Login IS NULL

Why is 'insert into select' statement in SQL inserting as a new row and not inserting correctly?

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)

Selecting rows where cells contain multiple values

Using Oracle SQL Developer 1.5.5.
I have a data table and I'm trying to select rows from the table (TABLENAME) where a column (COLUMN5) in the table contains multiple values. For example using the following command:
select * from TABLENAME where COLUMN5=''101','221','429''
I'm doing this because I want to select all rows in the table where the cell values in column 5 are '101','221',229'
An added complication is that in the table each of the values is within single quotation marks, separated by a comma. When run the above command I get an error message which says " SQL command not ended properly" I think this is something to do with the single quote marks and the multiple values because when i run the following command to select rows when column 5 is '443', I get a result:
select * from TABLENAME where COLUMN5='''443'''
Note that these values of '443' have single quotation marks around them in the table.
My question is what command will allow me to select rows with the multiple values in column 5? Help!
Let me know if you require any clarification
I think you can use FIND_IN_SET like below:
select * from accounts where FIND_IN_SET('101','101,102')
FIND_IN_SET ignore the single quotes
you just need to escape the quotes
create table so_col5 (a integer, col5 varchar2(25));
insert into so_col5 values (1, 'abcd');
insert into so_col5 values (2, 'abcd');
insert into so_col5 values (3, 'abcd');
Insert into SO_COL5 (A,COL5) values (4,'''101'',''221'',''229''');
commit;
select * from so_col5 where col5 = '''101'',''221'',''229''';
It took me a few tries, testing selecting the string from dual, but then I remembered I could just cheat - and export the result from the GRID as an INSERT Script and copy/paste the quoted string for that value and use that in my SELECT.
You're using v1.5, which is ANCIENT. Highly suggest you upgrade for an optimal experience.
You can also use the Q function (docs) which you can pass a string and Oracle handles the quote escaping for you. Here's a scenario you can play with on LiveSQL.

Hive split a string and insert into table

Here my question. I have a long string in a table, now I want to split it, and insert the result from splitting into another table.
for example.
INSERT TABLE table1
SELECT
split(result, ';')[0],
split(result, ';')[1],
...
FROM table2
WHERE ...
suppose result is a long string delimited by ';'
My query is not right, How can I fix it?
The only problem I can see is the first line. Alter it like so..
INSERT INTO TABLE table1
...

derby sql insert

I'm trying to insert values in my database
This is the statement I try to execute:
insert into leverancier (id,naam,straat,nr,postcode,plaats,telefoon)
values (1,"stef","bosstraat",88,9240,"Zele",null);
I get the following error:
ERROR 42X04: Column 'stef' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'stef' is not a column in the target table.
What is the problem?
To insert a string, like "stef", don't use double quotes but single quotes: 'stef'. Here's how the statement should be:
INSERT INTO leverancier
(id, naam, straat, nr, postcode, plaats, telefoon)
VALUES
(1,'stef', 'bosstraat', 88, 9240, 'Zele', NULL);
The error you get Column 'stef' is either not in any table ... is because double quotes are used for table and column names. So reading "stef", the parser assumes you are referring to a column named stef.