How to change existing data format through query in MySQL - mysql

I have more than 50 000 records in my database, but problem is that contact number is saved with '-' format.
How I can update all records so contact number will be updated without '-'?

You can use REPLACE(COLNAME, find_string, replace_with) in UPDATE Statement
UPDATE Table_Name
SET CONTACT_NO_1 = REPLACE(CONTACT_NO_1, '-', '')
Demo
http://sqlfiddle.com/#!9/d0cf5c/1

Related

MySQL Query: UPDATE and/or APPEND

I have a temporary table that I use to insert into the master db.
The temp table is named "temp_table"
The master table is "master"
I currently use the following command to update "master"
SELECT COUNT(*) FROM master;
SHOW COLUMNS FROM master;
INSERT INTO master
SELECT * FROM temp_table
ON DUPLICATE KEY UPDATE email = VALUES(email), phone = VALUES(phone)
Now, I want to be able to append field (counter) from the "temp table" into "master." The field already exists in both tables and I just want to be able to update or append it.
"counter" field in master may be empty or it may contain a number value already.
In cases where the value exists, it should append separated by a comma. Format (88,89,90)
In cases where the it's empty, it should update (88)
Thank you in advance.
I think you want:
on duplicate key update
email = values(email),
phone = values(phone),
counter = case when counter is null
then values(counter)
else concat(counter, ',', values(counter))
end
You can also phrase this with coalesce(), although the expression might be a bit more complicated to understand:
on duplicate key update
email = values(email),
phone = values(phone),
counter = concat(
coalesce(concat(counter, ','), ''),
values(counter)
)

How to edit all the data in an entire column?

In my MySQL database, I have a lot of data with the timestamp in this format:
2017.07.13 - 12:00:00:000
I want to change it to be in this format:
2017-07-13T12:00:00:000-0400
I know I need SELECT to get the data from the table and UPDATE to change it to a new record. But I don't know how to edit the timestamp using SQL commands.
How can I edit a string using SQL commands?
You don't need to run a select prior to an update. The update has the data already available. You could do something like this:
update table
set column = concat(replace(replace(column, ' - ', 'T'), '.', '-'), '-0400')
to alter the format of your all dates in the column column of the table table.
Demo: http://sqlfiddle.com/#!9/2699e9/2 (using select because the update wouldnt show anything)
https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_concat
https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace
https://dev.mysql.com/doc/refman/5.7/en/update.html
If you only want to update 1 specific row use a where clause to limit the update affects.
Should work (tested in SQL-Server)
Update tableName SET col = Replace(col," - ","T") + '-0400'
If + is not work for mysql then try CONCAT
Update: Solution for MySQL is
UPDATE tableName SET col = CONCAT(REPLACE(REPLACE(col, ' - ', 'T'), '.', '-'), '-0400')

MYSQL split and update record for the same column

I have a field in MySQL which contains a field named First name. Erroneously, the field was updated with Surname and First name with comma(,) between the names.
I want to update that filed running a MySQL command.
For example, my records are
Jack, Kallis
Mathew, Heyden
After the update command the fields will look like,
Kallis
Heyden
Thanks in advance.
Liton
Use SUBSTRING_INDEX
Select SUBSTRING_INDEX(name, ',', -1 ) From table
For update :-
Update table SET name=SUBSTRING_INDEX(name, ',', -1 )
Read more

SQL TIME change format in Database?

In my Database I have a column "length" from type TIME where the values are like 02:20:00 but I want them all to be 00:02:20
Is there any way to do?
I have over 5000 rows so I can't change it with UPDATE.
update your_table
set length = time(concat('00:', hour(length), ':', minute(length)))
SQLFiddle demo

how do you increment a field in mysql?

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.