How to add to an existing record in SQL?

Can someone tell me how to add to SQL? I've been trying to figure this out all day. This is what I have so far:

update table1 set field1 = field1 + '123456' where field2 = '12' 

Sorry, I forgot to mention that I am updating more than one field in the instructions.

+6
source share
4 answers

Your expression should work until field1 is null or the data being added is not null.

Something like this might help when field1 is null.

 update table1 set field1 = ISNULL(field1, '') + '123456' where field2 = '12' 
+13
source

in Oracle, string concatenation is as follows:

 field1 = field1 || '12345' 
+4
source

Your question is a bit confusing because you say append, but your example is really just a given operation:

 update table1 set field1 = '123456', field2 = '' where field2 = '12' 

if you really add it will depend on your database, but the concatenation of the search string is for reference.

 update table set field1 = concat(field2, '3456') where field2 = '12' 
0
source

here is the difference between varchar concatenation and adding an integer, you seem to continue varchar concatenation, you may need to use CAST and CONVERT (Transact-SQL) to add your numbers

example 1 w / integers:

 DECLARE @table1 TABLE(field1 int, field2 int) INSERT INTO @table1 VALUES (123456, 12) SELECT 'before' as 'before', * FROM @table1 UPDATE @table1 SET field1 = field1 + 123456 WHERE field2 = 12 SELECT 'after' as 'after', * FROM @table1 

example 1 result:

int addition

example 2 w / varchar:

 DECLARE @table2 TABLE(field1 varchar(50), field2 varchar(2)) INSERT INTO @table2 VALUES ('123456', '12') SELECT 'before' as 'before', * FROM @table2 UPDATE @table2 SET field1 = field1 + '123456' WHERE field2 = '12' SELECT 'after' as 'after', * FROM @table2 

Example 2:

varchar concat

0
source

Source: https://habr.com/ru/post/890262/


All Articles