19

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

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

Sorry, I forgot to mention that I'm updating more than one field in the statement.

7
  • 3
    Are you finding that this doesn't work? In short, if field1 is a varchar/nvarchar then what you've written would append. Commented Jun 10, 2011 at 22:31
  • Yes, field1 is a varchar(max) but it won't append. Commented Jun 10, 2011 at 22:33
  • If you are appending to a string, have you tried concat? eg, update table1 set field1 = field1 concat '123456' where field2 = '12' Commented Jun 10, 2011 at 22:34
  • 2
    What DBMS are you using? SQL is not (necessarily) MS SQL-Server. By the way, why have you tagged ASP.Net, it seems not being related to ASP.NET at all? Commented Jun 10, 2011 at 22:36
  • 1
    Please post the actual code, SQL flavor you are using, the result/error you get, and the expected result. Commented Jun 10, 2011 at 22:37

5 Answers 5

31

Your statement should work as long as field1 is not null or the data to be appended is not null.

Something like this could help in the case where field1 is null.

update table1 set field1 = ISNULL(field1, '') + '123456' where field2 = '12'
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. That was my problem.
Good call on catching that null would break it
5

Your question is a bit confusing because you are saying append but your example is really just a set operation:

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

if you were actually appending it would depend on your database but lookup string concatenation for reference.

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

4 Comments

Reread the question... set field1 = '123456' is not the same as set field1 = field1 + '123456'
@lazydba Agreed, but that was not the point I was making if you read the first sentence of my answer.
OK... just semantics then. His "set" operation is attempting to "append" field1 with what is already in field1 and something else.
the concat() worked for me with postgres. thanks!
5

in Oracle, the string concatenation goes like this:

field1 = field1 || '12345'

Comments

1

If you are working with MySQL the query can be simplified as

UPDATE table set field1 = CONCAT('123456', field1) where field2 = '12' 

1 Comment

Strictly speaking, the parameters of the CONCAT() should be swapped if we are to follow the example put by OP
0

here are the differences between varchar concatenation and integer addition, you appear to have varchar concatenation going on, 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 results:

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 results:

varchar concat

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.