<p>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.</p>

<p>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?</p>

[{'answer_id': 17627, 'author': 'Matt Mitchell', 'author_id': 364, 'author_profile': 'https://Stackoverflow.com/users/364', 'pm_score': 6, 'selected': True, 'text': "<p>The solution is:</p>\n\n<pre><code>UPDATE tablename SET [filename] = RTRIM([filename]) + '.jpg' WHERE id &gt; 50\n</code></pre>\n\n<p>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.</p>\n"}, {'answer_id': 17629, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 2, 'selected': False, 'text': "<p>Nice easy one I think.</p>\n\n<pre><code>update MyTable\nset filename = filename + '.jpg'\nwhere ...\n</code></pre>\n\n<p>Edit: Ooh +1 to @MattMitchell's answer for the rtrim suggestion.</p>\n"}, {'answer_id': 17636, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 3, 'selected': False, 'text': '<p>MattMitchell\'s answer is correct if the column is a CHAR(20), but is not true if it was a VARCHAR(20) and the spaces hadn\'t been explicitly entered.</p>\n\n<p>If you do try it on a CHAR field without the RTRIM function you will get a <em>"String or binary data would be truncated"</em> error.</p>\n'}, {'answer_id': 77596, 'author': 'Amy B', 'author_id': 8155, 'author_profile': 'https://Stackoverflow.com/users/8155', 'pm_score': 1, 'selected': False, 'text': "<p>If the original data came from a char column or variable (before being inserted into this table), then the original data had the spaces appended before becoming a varchar.</p>\n\n<pre><code>DECLARE @Name char(10), @Name2 varchar(10)\nSELECT\n  @Name = 'Bob',\n  @Name2 = 'Bob'\n\nSELECT\n  CASE WHEN @Name2 = @Name THEN 1 ELSE 0 END as Equal,\n  CASE WHEN @Name2 like @Name THEN 1 ELSE 0 END as Similiar\n</code></pre>\n\n<p>Life Lesson : never use char.</p>\n"}, {'answer_id': 79750, 'author': 'Ricardo C', 'author_id': 232589, 'author_profile': 'https://Stackoverflow.com/users/232589', 'pm_score': 1, 'selected': False, 'text': '<p>The answer to the mystery of the trailing spaces can be found in the ANSI_PADDING</p>\n\n<p>For more information visit: <a href="http://msdn.microsoft.com/en-us/library/ms187403.aspx" rel="nofollow noreferrer">SET ANSI_PADDING (Transact-SQL)</a></p>\n\n<p>The default is ANSI_PADDIN ON. This will affect the column only when it is created but not to existing columns.</p>\n\n<p>Before you run the update query, verify your data. It could have been compromised.</p>\n\n<p>Run the following query to find compromised rows:</p>\n\n<pre><code>SELECT *\nFROM tablename \nWHERE LEN(RTRIM([filename])) &gt; 46 \n-- The column size varchar(50) minus 4 chars \n-- for the needed file extension \'.jpg\' is 46.\n</code></pre>\n\n<p>These rows either have lost some characters or there is not enough space for adding the file extension. </p>\n'}, {'answer_id': 80137, 'author': 'Dr8k', 'author_id': 6014, 'author_profile': 'https://Stackoverflow.com/users/6014', 'pm_score': 1, 'selected': False, 'text': '<p>I wanted to adjust David B\'s "Life Lesson". I think it should be "never use char for variable length string values" -> There are valid uses for the char data type, just not as many as some people think :)</p>\n'}]
['https://Stackoverflow.com/questions/17624', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/364/']