I have a database with several hundred fields but my data structure is wrong. It is currently in uk format as follows:
d/m /y
01/01/85
01/01/96
23/12/87
What would be the most efficient way to change the dates in bulk to sql standard of year/month/day
eg.
02/01/85 --> 1985/01/02
Create a new DATE type column in your table:
ALTER TABLE myTable ADD newColumn DATE;
Use MySQL's STR_TO_DATE() function to parse the existing values into your new column:
UPDATE myTable SET newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y');
Change your codebase to use the new column. If this can't happen immediately, you could define BEFORE INSERT and BEFORE UPDATE triggers to keep the new column consistent with the old one:
CREATE TRIGGER myTable_bi BEFORE INSERT ON myTable FOR EACH ROW
SET NEW.newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y');
CREATE TRIGGER myTable_bu BEFORE UPDATE ON myTable FOR EACH ROW
SET NEW.newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y');
Drop the old column:
ALTER TABLE myTable DROP oldColumn;
select date_format(date, '%Y-%m-%d') use this to change it to required format.I have used date_format function. You can get more information about date_format here
Related
I find it difficult to fill a new column that I just added to an already existing table in SQL
I tried the Insert command, to fill in the table afresh, but couldn't see it true because the table has 1445 rows
You could specify a default value when you add the column:
ALTER TALE mytable ADD COLUMN new_column VARCHAR(10) DEFAULT 'my_value'
Alternatively, once you added the column, you could use an update statement:
UPDATE mytable
SET new_column = 'my_value'
Just like the answer above you can use ALTER TABLE to add a new column
ALTER TABLE dummy
ADD temp_column VARCHAR(50);
Then you can use the update and add a where statement to be more specific
UPDATE dummy
-> SET temp_column = 'DEFAULT'
-> where id > 10;
Alternatively you could try using Five, you can import your database in it as an SQL dump, it actually allows you to do way more with new columns, like copy data from an existing field in the table or fill in the values from a query
here is an image of Five prompting you to fill in values for a new column
there are other things as well like you can write queries directly in Five, reuse the queries and the tables can be generated with simple point and click.
Disclaimer: I work for Five
I have a MYSQL database that is on a remote shared server.
One of my columns in a table requires to populate automatically with the CURRENT_TIMESTAMP when the record is being populated with other data.
The database uses the SYSTEM time which is "UTC +00:00", but I need this to be "UTC +01:00"
I cannot use "SET GLOBAL time_zone = '+01:00';" as I don't have the priveliges (understandably), and understand there's no way to set a time_zone just at database level (beyond a session instance, this solution needs stick).
Is there a way that I can have my column populate with "CURRENT_TIMESTAMP + ADD ONE HOUR", like a calculation? As I'm looking for a solution at DB level to this.
I'm not db profficient, but appreciate any advice.
you can create a trigger that launched each time you insert in that column
DELIMITER $$
CREATE TRIGGER update_count
AFTER UPDATE ON table_name (time)
UPDATE table_name
SET HOUR(column_name) = SEC_TO_TIME((UNIX_TIMESTAMP(HOUR(column_name))+1)*60)
WHERE time= NEW.time;
END;
$$DELIMITER ;
Look for the function CONVERT_TZ
mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
-> '2004-01-01 13:00:00'
mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');
-> '2004-01-01 22:00:00'
It might be of help to what you need, here's the docs:
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_convert-tz
You will only change time zone,
Example:
CREATE TABLE IF NOT EXISTS test_timestamp (
t1 TIMESTAMP
);
SET time_zone='+00:00';
INSERT INTO test_timestamp
VALUES('2008-01-01 00:00:01');
SELECT
t1
FROM
test_timestamp;
t1='2008-01-01 00:00:01'
SET time_zone ='+03:00';
SELECT t1
FROM test_timestamp;
t1='2008-01-01 03:00:01'
I have a table called table1 with three columns, one of which is Date_Of_Call which is of datetime type with the data in PDT. I basically need to convert the data from PDT to UTC and put the UTC converted dates into a new column in the existing table. I added a new column with:
alter table table1 ADD Date_Of_Call_UTC DATETIME;
I am able to get the proper time conversion with this select statement:
select CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00') from table1;
The issue I am having is trying to use an update command to take the results of the select statement and put them in the new Date_Of_Call_UTC column. Any thoughts of how to do this?
I tried the below statement and a few variations but can't quite figure out what I need to do:
update table1 set table1.Date_Of_Call_UTC = (select CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00') from table1);
Any assistance is appreciated!
this one should work:
update table1
set table1.Date_Of_Call_UTC = CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00');
NOTE: dates are usually stored already as UTC in mysql, but during output they can be displayed with offset applied, read about it: http://dev.mysql.com/doc/refman/5.0/en/datetime.html and http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Merge date from one datetime and time from another datetime
How can I physically merge two MySQL column/fields into one column/field without losing any data on it
Specifically I will be combining a DATE and TIME column/field
thanks
Add your timestamp field, initialize it with the timestamp function, and then once you feel safe, drop the old columns. Something like this:
alter table your_table add timestamp_column timestamp;
update your_table set timestamp_column = timestamp(date_column, time_column);
-- Pause and make sure everything is okay.
-- Take your time, the database will wait.
alter table your_table drop column date_column;
alter table your_table drop column time_column;
alter table your_table modify timestamp_column timestamp not null;
By "merge", do you mean "add"? It sounds like you've got the date and time in separate fields, and so putting them together is the same thing as adding them.
If that's the case, you can use the MySQL's ADDTIME() function:
mysql> SELECT ADDTIME(aDate, aTime) FROM aTable;
If you mean to concatenate them, then MySQL has a CONCAT() function... But it doesn't seem suited for your purpose.
--edited--
If it's another column you're after, it's a two step process:
-- Add the column
mysql> ALTER TABLE aTable ADD COLUMN aDateTime DateTime DEFAULT NULL;
-- Insert the data
mysql> UPDATE aTable SET aDateTime = ADDTIME(aDate, aTime);
But after this point, you'll have to maintain it in all three columns. That is, aDateTime won't automatically update when you add/change aDate or aTime.
i dont wanna if you want to create a new field but just in case you wanna do it
alter table t add yournewfield datetime not null;
UPDATE table t SET younerwfield = addtime(t.date,t.time);
Hi I would like to set and forget two fields for tracking the date the record was added and also the date the record was last modified in a mySQL database.
I am using "ON UPDATE CURRENT_TIMESTAMP" and was hoping I would just change UPDATE to INSERT.
No luck however. Can anyone give me the heads up on the best way to achieve this? - preferably inside the database itself.
This assumes MySQL 5. Simply add two triggers:
create table foo (a1 INT, created timestamp, updated timestamp) engine=innodb;
DELIMITER |
CREATE TRIGGER foo_created BEFORE INSERT ON foo
FOR EACH ROW BEGIN
SET new.created := now();
SET new.updated := now();
END;
|
CREATE TRIGGER foo_updated BEFORE UPDATE ON foo
FOR EACH ROW BEGIN
SET new.updated := now();
END;
|
DELIMITER ;
insert into foo (a1) values(7);
select * from foo;
update foo set a1=9;
You basically need both columns to be setup as timestamps with default values of CURRENT_TIMESTAMP. Unfortunately, this is not allowed in MySQL:
Error Code: 1293
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
You can't have two timestamp columns, even though you need one to only have a default value of CURRENT_TIMESTAMP, and the other one to be UPDATE CURRENT_TIMESTAMP, this is still not allowed.
Your best bet here would be to specify as so:
CREATE TABLE `test` (
`addedDate` dateTime,
`lastModified` timestamp on update CURRENT_TIMESTAMP
)
Unfortunately, you'll have to set the 'addedDate' manually on insert using the NOW() function.
mySQL has a NOW() function you can use, see the tutorial at Tutorials Point that can help you put it in place.
You could add a DATETIME column and set it when you create the row of data. That will serve as the date the record was added.
Next, add a TIMESTAMP column:
Automatic updating of the first TIMESTAMP column in a table occurs under any of the following conditions:
You explicitly set the column to NULL.
The column is not specified explicitly in an INSERT or LOAD DATA INFILE statement.
The column is not specified explicitly in an UPDATE statement and some other column changes value. An UPDATE that sets a column to the value it does not cause the TIMESTAMP column to be updated; if you set a column to its current value, MySQL ignores the update for efficiency.
The TIMESTAMP column will take care of your record modified date.