4

Does anyone know how I would go about concatenating a string in SQL Server 2005.

What I mean is something like the following scenario.

I have a nvarchar(MAX) column in a SQL Server 2005 database.

Lets say the column has a value of "A" and I want to add "B" making "AB", what is the simplest way to go about this. Will I need to do a Select, concatenate the two values in code and then update the column? Or is there a more nifty way to do this?

Any pointers much appreciated.

2 Answers 2

8

In T-SQL:

     UPDATE table SET col = col + 'B' WHERE (PREDICATE THAT IDENTIFIES ROW)

If you were using Oracle it would be:

     UPDATE table SET col = col || 'B' WHERE (PREDICATE THAT IDENTIFIES ROW)
Sign up to request clarification or add additional context in comments.

2 Comments

T-SQL is what th OP wanted, see the tags.
Oracles pipe operators are unique enough that it was worth mentioning for other people who search for this.
2

You can do something like this

DECLARE @Table TABLE(
        Col VARCHAR(MAX)
)

INSERT INTO @Table (Col) SELECT 'A'

SELECT  Col + 'B'
FROM    @Table

UPDATE @Table
SET Col = Col + 'B'

SELECT * FROM @Table

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.