SQL TIME change format in Database? - mysql

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

Related

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

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

How to insert * symbol in mysql using php

i have one field which contain data like 4563******3245. when i execute my sql query it is inserted successfully. but in mysql database it is showing only 4563
my sql query is:
insert into mytable ('myfield') values ('4563******3245');
Can any one tell me where is the problem.
thank you.
insert into mytable ('myfield') values ('4563******3245');
it is working fine
make sure your column type should be varchar
You can't store text in a column of a number data type. You have to change your data type to char(14).
ALTER TABLE your_table MODIFY myfield CHAR(14);
If your running that particular query, and only 4563 is being inserted, it would suggest your column type for "myfield" is set as some variant or length of "int" rather than char(22) (CCN's are not always 16 digits, some can be 20 or 22).
you should probably switch the column type to make sure the data inserts correctly to something like
ALTER TABLE mytable MODIFY myfield char(22);

Changing the column Type in SQL

Hey I have an SQL Table which has a column for storing date but the date column has a type varchar. I want to change the type to date but I don't want the actual data to be lost in that column. How can I achieve that.
Manually taking a backup of the table and then entering each entry? or there is some other cool way to do it ? Actually the data is huge
Thanks
My way of doing this:
(1) Add a new column:
ALTER TABLE yourtable
ADD COLUMN `new_date` DATE NULL AFTER `views`;
(2) Update the new column
UPDATE yourtable SET new_date = old_date;
Take care of the datas formatting in old_date. If it isn't formatted yyyy-mm-dd, you might have to STR_TO_DATE or some string-replacements in this UPDATE-statement here to fit your purposes.
Example:
If your data looks like this: mmmm dd, yyyy, hh:mm (p.e. May 17, 2012, 8:36 pm) , you can update like this:
UPDATE yourtable
SET new_date = STR_TO_DATE(old_date, "%M %e, %Y");
STR_TO_DATE basically reverse engineers string data to a date value.
(3) Delete the old column
ALTER TABLE yourtable
DROP COLUMN `old_date`;
(4) Rename the new column
ALTER TABLE yourtable
CHANGE `new_date` `old_date` DATE NULL;
Done!
What about:
1) Adding a new column with the right type
2) Updating your new column with the parsed dates
3) Removing the old column
Update to fill the date pattern parsing requirement:
SELECT STR_TO_DATE('May 17, 2012, 8:36 pm','%M %d, %Y');
Add a new column with data type you want, then run an UPDATE query to copy the data from the old column to the new column. and then delete the the old column.
Note that perhaps you will have to use the CONVERT function to convert the date string into a datetime.
Use this query:
ALTER TABLE tablename MODIFY COLUMNNAME Datatype
e.g.
ALTER TABLE Users MODIFY RegisterDate DateTime

mysql change date format

I have a date field (tinytext) holding date information in format of "dd-mm-yy" e.g 07-01-90. Using a mysql query I want to change it to yyyy-mm-dd date format. I tried the code below but nothing happens.
mysql_query("UPDATE Table SET date=STR_TO_DATE('date','%Y,%m,%d')");
You're using the correct function STR_TO_DATE(str,format) to achieve the goal, but you're making two mistakes:
In your query the format argument does not match the string expression format. You said it's in dd-mm-yy format while you passed %Y,%m,%d (comma separated) to the format argument. The query will return a "incorrect datetime value" error. You should use %d-%m-%Y.
You can't change data type of a column on the fly, by setting different type of the value being passed. You have to first update the values and then change data type for column.
So, summarizing:
mysql_query("UPDATE `Table` SET `date` = STR_TO_DATE(`date`, '%d-%m-%Y')");
mysql_query("ALTER TABLE `Table` CHANGE COLUMN `date` `date` DATE");
Additionally, consider switching to the recommended PDO extension in place of old and slowly deprecated mysql extension.
Error in your query
is STR_TO_DATE(date, '%Y-%m-%d')
mysql_query("UPDATE Table SET date=STR_TO_DATE(date,'%Y-%m-%d')");
Try this:
INSERT INTO table(date_field) VALUES(STR_TO_DATE('December 8, 2010','%M %d,%Y'));
Try it with DATE_FORMAT() function.
mysql_query("UPDATE Table SET date=DATE_FORMAT(date,'%Y,%m,%d')");
To display 2 digit year
mysql_query("UPDATE Table SET date=DATE_FORMAT(date,'%y-%m-%d')");
To display 4 digit year
mysql_query("UPDATE Table SET date=DATE_FORMAT(date,'%Y-%m-%d')");
I'd say you have to do this:
UPDATE table_name SET date = DATE_FORMAT('date', %Y-%m-%d);
If you are using my_sql with php, you can use date function