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');
Related
I am a beginner in SQL. Currently, I am working with a SQL database that has two columns. The first column specifies the id. The second column specifies a list of people separated by the delimiter "#d#" So, the column looks something like "John#d#Jack#d#Prince"
I need to delete a specific name from this list. Suppose, I am deleting prince from the list. I want my row to look like John#d#Jack after the delete operation. I was researching solutions for this procedure and I found couple resources. I learned about this approach "UPDATE TABLE SET columnName = null WHERE YourCondition" As a result, I can change the whole column to null, but I don't know how to retain the string and only delete the specified value.
You can use replace function
update yourTable set yourField = replace(replace(yourField, 'Prince', ''), '##' , '#') where yourCondition;
First replace "delete" the name you want to, second replace "delete" deleted name's delimiter.
You can do this using:
update t
set list = trim(both '#' from replace(concat('#', list, '#'), concat('#', 'prince', '#'), '#'))
where concat('#', list, '#') like concat('%#', 'prince', '#%');
You can replace 'prince' with a variable or whatever you want to replace.
If I am not mistaken the command you are looking for is
UPDATE TABLE set columnName = "John#d#Jack" WHERE YourCondition
Or do you want a more general approach?
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
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 need a SQL update statement for updating a particular field of all the rows with a string "test" to be added in the front of the existing value.
For example, if the existing value is "try" it should become "testtry".
You can use the CONCAT function to do that:
UPDATE tbl SET col=CONCAT('test',col);
If you want to get cleverer and only update columns which don't already have test prepended, try
UPDATE tbl SET col=CONCAT('test',col)
WHERE col NOT LIKE 'test%';
UPDATE tablename SET fieldname = CONCAT("test", fieldname) [WHERE ...]
Many string update functions in MySQL seems to be working like this:
If one argument is null, then concatenation or other functions return null too.
So, to update a field with null value, first set it to a non-null value, such as ''
For example:
update table set field='' where field is null;
update table set field=concat(field,' append');
That's a simple one
UPDATE YourTable SET YourColumn = CONCAT('prependedString', YourColumn);
UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column1) where 1
UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column2) where 1
UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column2, 'newtring2') where 1
We can concat same column or also other column of the table.