How to edit all the data in an entire column? - mysql

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')

Related

create new column with conditional values from existing column in MySql workbench?

i have a column in my MySql table named "details" like this:
now i want to add another column named "name" which will contain only the first word of each cell of column "details", like this:
so far i have done this:
select
substring(details, 1, instr(details, ' '))
from
my_table
now how to add this to a column "name"?
Please try this.
select
substring(details, 1, instr(details, ' ')) As Name
from
my_table
use position() function
DEMO
select
substring(details, 1, POSITION(' ' IN details))
from
my_table
OUTPUT:
value
Wellcare
ALSO locate() can be used
select
substring(details, 1, LOCATE(' ', details))
from
my_table
You can use SUBSTRING_INDEX to extract the first word of each value. For example:
SELECT SUBSTRING_INDEX('Wellcare Value (HMO-POS)', ' ', 1) AS name
Output:
Wellcare
To add this to your table, you have a couple of options. If you are running MySQL 5.7.6 or later, you can use a generated column as described in this question and the manual e.g.
ALTER TABLE mytable ADD COLUMN name VARCHAR(50) AS (SUBSTRING_INDEX(details, ' ', 1))
If you want to save storage at the expense of fetch time, declare the column as VIRTUAL, otherwise declare it as STORED.
You can also create a VIEW which includes this expression as a column. This is similar to a VIRTUAL generated column but will also work on versions of MySQL which don't support generated columns:
CREATE VIEW mytable_view AS
SELECT *, SUBSTRING_INDEX(details, ' ', 1) AS name
FROM mytable
The final option would be to create an actual column and then update it via a trigger, but both the preceding options are preferable.

How to change existing data format through query in 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

SQL - Split table based upon a column's content

I have a large database (~50,000 rows) with 20 columns, and I want to "split" the data based upon the values in the third column (called FEATURE_CLASS). The values of FEATURE_CLASS are all of type VARCHAR(), and I want to create however many tables I'd need to replace the single, large table with many smaller tables each entitled with whatever the original table's FEATURE_CLASS value was.
Not sure of the best way to go about this, I was thinking something along the lines of creating a temporary table which would serve as an index, each row carrying a unique value of FEATURE_CLASS, then to iterate over the temp table and perform copying operations for each row of the temp table. I'm not sure really where to go from here, any help/ideas would be appreciated. Thanks!
The basic idea is to create a result set of statements that will create the tables for you and populate with the correct data. Run the below to generate the statements. You can then execute these statements manually (copy/paste) or from within a script.
SQL Server Example
SELECT DISTINCT
'SELECT * INTO [' + TableName.FEATURE_CLASS + '] FROM TableName WHERE FEATURE_CLASS = ''' + TableName.FEATURE_CLASS + ''';'
FROM
TableName
If you have any special characters in the FEATURE_CLASS column, you might want to consider removing them in the script above to prevent table names that are either invalid or tough to work with.
For example:
...
'SELECT * INTO [' + REPLACE(TableName.FEATURE_CLASS, '.', '') + '] FROM TableName WHERE FEATURE_CLASS = ''' + TableName.FEATURE_CLASS + ''';'
...
MySQL Example
SELECT DISTINCT
CONCAT('CREATE TABLE `', DB1.FEATURE_CLASS,
'` AS SELECT * FROM DB1 WHERE FEATURE_CLASS = ''',
DB1.FEATURE_CLASS, ''';') AS statements
FROM DB1;
This will give you a MySQL command something like this:
CREATE TABLE `feature_class_value` AS
SELECT * FROM DB1
WHERE FEATURE_CLASS = 'feature_class_value';
Check out the MySQL docs for more info on CREATE TABLE SELECT options https://dev.mysql.com/doc/refman/5.7/en/create-table-select.html.

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.

Adding text to each column of MYSQL Database

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...’);