I have the below sample data and I would like to remove the word "Division" from the data set.
Table A:
Central Division
North Division
East Division
You may looking for simple update statement
Update T
Set ColumnName=RTRIM(REPLACE(ColumnName,'Division',''))
FROM TableName t
WHERE ColumnName like '%Division%'
You can use this:
Update A
Set ColumnName=Ltrim(REPLACE(ColumnName,'Division',''))
FROM TableName A
WHERE ColumnName like '%Division%'
Or for null :
Update A
Set Column = Null
Where Column Like '%Division%'
Division is the last word on every value
As the word is always at the end and of a fixed length the best way is to simply chop off the last 9 characters:
update t set fld = left(fld, len(fld) - 9)
That do all of the job for you- Removing all ' Division' from sample table.
DECLARE #Table Table
(
nameOfColumn VARCHAR(100)
)
INSERT INTO #Table VALUES
('Central Division'),
('North Division'),
('East Division')
UPDATE #Table
SET nameOfColumn = REPLACE(nameOfColumn,' Division','')
FROM #Table
SELECT * FROM #Table
Related
I have a column named 'unit' in my db. The value of all (several hundred) entries are entered as "12.Z" or "16.Z" etc.
I would like to update all values to "12 OZ" or "16 OZ".
I.e. if the value is 12.Z it should be changed to 12 OZ.
I have no problem updating multiple rows in one query, however I'm not sure how to begin (or end) this query.
Use this query:
UPDATE <TABLENAME> SET <COLUMNNAME>=REPLACE(<COLUMNNAME>, '.Z', 'OZ') WHERE <COLUMNNAME> LIKE '%.Z'
is the name of the table you want to update (remove the <> chars)
is the name of the column in the table you want to update (remove <>)
Replace is a MySQL function to replace characters in a string (https://www.w3resource.com/mysql/string-functions/mysql-replace-function.php)
LIKE is the search operator. You are only looking for values ending with .Z. The % is a wildcard to have anything at the beginning.
You can use an update as
You can use two separated query
update my_table
set my_col = '12 OZ'
where my_col = '12.Z'
;
update my_table
set my_col = '16 OZ'
where my_col = '16.Z'
;
or use a single query with case when
update my_table
set case when my_col = '12.Z' then '12 OZ'
when my_col = '16.Z' then '16 OZ'
where my_col in ('12.Z', '16.Z')
;
Update multiple rows in mysql
Syntax
"UPDATE table_name SET column_name1=' value[$array]', column_name2=' value[$array]' WHERE column_name=' value[$array]' ";
Please Try this link
Thanks
I have a database table, with a column that contains integers. Each entry is a phone number, and they are all missing a zero at the beginning.
e.g. I have 798514586,
785558999
I want to run a SQL query that will modify each entry by putting a zero infront of it.
So the result will be
0798514586,
0785558999
IS there such a query to do this?
Try this
Syntax:
UPDATE <table> SET <column_to_update> = CONCAT(<string_to_concat>, <column_to_update>)
Example:
UPDATE contacts SET phone = CONCAT('0', phone)
I suppose you dont't want to add leading zero if it already exists:
update TableName
set SomeColumn = concat('0', SomeColumn)
where SomeColumn not like '0%'
It's not a good idea to store phone numbers as INTs, it's better to use a VARCHAR here. I would suggest you do add a new varchar column:
ALTER TABLE numbers ADD COLUMN phone_new VARCHAR(15);
then you can use an UPDATE query:
UPDATE numbers
SET
phone_new = CONCAT('0', phone)
MySQL will automatically cast the number to a string, and using CONCAT you can add a leading zero.
You can try by this:
update tableName set fieldName = CONCAT('0',fieldName)
You can use LPAD :
Update _table set _col = LPAD(_col , 10, '0');
Say, you got a table of 100 records. And field age contains a some integers. And you want all those integers to be incremented by 1.
or you got a textfield called name and a bunch of names in there. And you want all those names to be prefixed as Mr..
Is there a way to achieve this in one SQL command?
The alternative would be to compile a recordset of these 100 recs and going thru a loop and then running an individual update statement.
Use the update command
update yourtable
set age=age +1
update yourtable
set name = 'Mr. ' + name
where gender='M'
UPDATE mytable SET age = age+1
UPDATE mytable SET name = CONCAT('Mr. ', name)
If MySQL is in ANSI mode – specifically, PIPES_AS_CONCAT, you can use 'Mr. ' || name instead.
I have a database table in MYSQL with around 1000 rows. In the table I have a column called 'overview'. In each row, this column has some value and at the end of that value I have a specific line (text) starting with: 'Source...'
Now what I want is, I want to remove this line from each column and replace it with some other text content.
I believe it can be accomplished with some smart query.
You can simply use REPLACE in your query like this
UPDATE your_table SET col_name = REPLACE(col_name , ‘Source...’, ‘new_val’)
WHERE col_name LIKE '%Source...';
Check Out the SQLFIDDLE.
MySQL database has a handy and simple string function REPLACE() that allows table data with the matching string (from_string) to be replaced by new string (to_string).
The syntax of REPLACE is:
REPLACE (text_string, from_string, to_string)
In your case, you can do this way:
UPDATE `tableName` SET `column` = REPLACE(column , 'Source...', 'Replaced Value')
Use Replace
update TBL
set overview = Replace(picture, 'Source..', 'replacement')
keep a backup of the table before anything.Or you can do it on a copy.
you can do this by following:
update table_name set col_name = replace(column_name , ‘Source...’, ‘Replaced String...’);
I have a scalar function that takes a two variables #input1 #input2 and it returns the value of #input1 and #input2 (actual thing is more complex but this distills the idea).
I want to update all rows in a table column using this function, passing the value 'abc ' for #input1 and using the column name in #input2, so my update statement would look something like:
update mytable set mycolumn = (select dbo.myfunc( 'abc ' , mycolumn ) )
-- prepend the literal 'abc ' to every row for column mycolumn
But this is of course not allowed.
I'm trying to perform some mass string handling on a couple of columns based on some string rules. All ideas appreciated.
Thanks.
UPDATE mytable
SET mycolumn = dbo.myfunc('abc', mycolumn)