Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update the field value of specific's column in SQL Server using Query in one shot?

I have Imported Data Form XLS my CMS. but finally realize that it has wrong data without any Extension at the end of the data, in One of the column of the table, so now I want to change the column data of multiple rows based on specific ID? How to do that using single SQL Query?

I just want to add '.jpg' at the end on these data.

Image AspxCommerce Error

like image 946
Milson Avatar asked May 22 '13 06:05

Milson


People also ask

How do you update a specific column value in SQL Server?

First, specify the table name that you want to change data in the UPDATE clause. Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.

How do you update a column NULL value in SQL?

UPDATE [table] SET [column]=0 WHERE [column] IS NULL; Null Values can be replaced in SQL by using UPDATE, SET, and WHERE to search a column in a table for nulls and replace them. In the example above it replaces them with 0.

Can we change the value in two columns using a single update query?

We can update multiple columns by specifying multiple columns after the SET command in the UPDATE statement. The UPDATE statement is always followed by the SET command, it specifies the column where the update is required.

How do you change the value of a column based on another column in SQL?

UPDATE table SET col = new_value WHERE col = old_value AND other_col = some_other_value; UPDATE table SET col = new_value WHERE col = old_value OR other_col = some_other_value; As you can see, you can expand the WHERE clause as much as you'd like in order to filter down the rows for updating to what you need.


5 Answers

UPDATE a
SET a.Imagepath = a.ImagePath + '.jpg'
FROM aspx_itemimages a
WHERE a.ItemId = yourid
like image 125
Andrey Gordeev Avatar answered Sep 27 '22 22:09

Andrey Gordeev


Try this one -

UPDATE aspx_itemimages
SET ImagePath = ImagePath + '.jpg'
--WHERE itemid = 755
like image 42
Devart Avatar answered Sep 27 '22 21:09

Devart


Try:

UPDATE ImagePath SET ImagePath = ImagePath + '.jpg'
like image 25
4b0 Avatar answered Sep 27 '22 22:09

4b0


According to your screenshot, some entries actually do end with .jpg. If you want to append .jpg only where it is absent, you could use a NOT LIKE predicate:

UPDATE  aspx_itemimages
SET     ImagePath = ImagePath + '.jpg'
WHERE   ImagePath NOT LIKE '%.jpg'
;

That will also prevent you from adding the extension multiple times if you run the query again accidentally.

like image 23
Andriy M Avatar answered Sep 27 '22 20:09

Andriy M


The above answers are correct but adding 2 strings returns 0, you will have to concat two strings. here is the example:

UPDATE a
SET a.Imagepath = concat(ImagePath , '.jpg')
FROM aspx_itemimages a
WHERE a.ItemId = yourid
like image 38
Raj Avatar answered Sep 27 '22 20:09

Raj