We know how to change data inside a field by using update command inside a query. Here we will try to learn how to replace part of the data without changing the entire data of the field. The SQL query we design will work like string replacement in any script we use. For this we will use replace command to replace a part of the data inside a field. Let us try to learn this with an example.
In one employee table ( name emp ) we have three fields, name designation and department. There are different designation given based on the name of their department like GM( finance ), Manager (HR), VP(Sales) etc. Now let us say that the department name Sales changed to Marketing in the organization. We can give a single update command in the field department to change the department name to Marketing where ever it is Sales. But the problem is changing the designation of GM( Sales ) to GM ( Marketing), the query should change only for 'Sales' inside department field and should not change their main designation like GM, VP or Manager. It should only change part of the designation saying about the particular function ( or department ).
Here is the table before any changes are applied.
name
designation
department
JOhn D
GM(Sales)
Sales
Nill K
GM(Finance)
Finance
Poul P
VP(Sales)
Sales
Kriss A
VP(HR)
HR
Ricky T
Manager(Sales)
Sales
Rolls N
Manager(IT)
IT
Now we will try to change the departments exists within the designation field. Before that let us change the department name to Marketing by using this simple update command.
In the above table, we can see the department name is changed. Now we have to apply replace query to change the department part of the designation field. The field data saying Manger( Sales) should change to Manager( Marketing) and Manager ( HR) should not change. Now here is the query
Remove commas , from string to convert to Integer column
In some tables we will have , ( comas ) as number formats and the column is used as text or varchar column. To apply any math calculation or to list in order by we have to convert the text or varchar column to integer column. Before converting we have to remove the , ( commas ) from the data and then apply changes. Here is the query to remove commas from the data.
Here p_view is the column having commas inside the data and y21_11 is the table name.
UPDATEy21_11SETp_view=REPLACE(p_view,',','')
After removing the commas the column is changed to int type.
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery,
and web development at plus2net. The tutorials focus on clear explanations, working
examples, and code that readers can test and adapt while learning.