What is the easiest way using T-SQL / MS-SQL to append a string to existing table cells?

Question :

What is the easiest way using T-SQL / MS-SQL to append a string to existing table cells?,

Answer :

I have a table with a ‘filename’ column.
I recently performed an insert into this column but in my haste forgot to append the file extension to all the filenames entered. Fortunately they are all ‘.jpg’ images.

How can I easily update the ‘filename’ column of these inserted fields (assuming I can select the recent rows based on known id values) to include the ‘.jpg’ extension?

,

The solution is:

UPDATE tablename SET [filename] = RTRIM([filename]) + '.jpg' WHERE id > 50  

RTRIM is required because otherwise the [filename] column in its entirety will be selected for the string concatenation i.e. if it is a varchar(20) column and filename is only 10 letters long then it will still select those 10 letters and then 10 spaces. This will in turn result in an error as you try to fit 20 + 3 characters into a 20 character long field.

Read More  FogBugz compared to OnTime

That’s the answer What is the easiest way using T-SQL / MS-SQL to append a string to existing table cells?, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you

Read More  Import Namespace System.Query