Hello there I am trying to update an existing column and set the year column from 1/1/2019 to just 2019. I am getting a syntax error not sure how to do this. Thank You
UPDATE TABLE set year = YEAR(date_format(STR_TO_DATE(year,'%d/%m/%Y')))
WHERE id > 0;
I instead suggest that you don't store the year by itself, but rather create a new bona fide date column, and use STR_TO_DATE to populate it with your current data:
ALTER TABLE yourTable ADD COLUMN new_date DATETIME;
UPDATE yourTable
SET new_date = STR_TO_DATE(year, '%d/%m/%Y');
Afterwards, you may drop the previous text column year, assuming you don't need it anymore:
ALTER TABLE yourTable DROP COLUMN year;
Note that if you want to obtain the year from new_date, then you may easily do so, e.g.
SELECT new_date, YEAR(new_date) AS year
FROM yourTable;
This will Work For You Try this
UPDATE `Table` SET`Year`= STR_TO_DATE(year(CURDATE()),"%Y");
This will be only set the year but not the whole date
----------------------Note-------------------------
if you want to totally drop the month and the day change the datatype from date to year of your column in mysql if you want to know more about the Year datatype check out this link : https://dev.mysql.com/doc/refman/8.0/en/year.html
Okay I figured it out seems way more complicated than it should be. You need to convert the date to unix_time and the from_unixtime you need to select the year
UPDATE my_table
set year=YEAR(FROM_UNIXTIME(UNIX_TIMESTAMP(STR_TO_DATE(year, '%d/%m/%Y'))))
WHERE id > 0;
Related
I have a column "date" with data type date and format "YYYY-MM-DD".
I would like to create a new column having only the year "YYYY".
I tried YEAR() and EXTRACT() functions but to my understanding those are queries and I cannot insert them into a column later on.
Any thoughts on that?(please keep in mind that I am novice at best)
First you need to update your table's schema with an ALTER TABLE statement as follows:
ALTER TABLE <your_tab_name> ADD COLUMN `year` INT;
Then you can use an UPDATE statement and your function YEAR to update your newly created field with the extracted year value.
UPDATE <your_tab_name>
SET `year` = YEAR(`date`)
I'm trying to update a column from a MySQL table so that it changes to current timestamp when the new date from another column is greater than the existing date from that column, and stays the same otherwise.
For example, let's say that I have a table with two columns, enddate and enddate_modif. If the date in enddate in the updating record is greater than the date in enddate in the existing table, I want enddate_modif to change to current timestamp. Otherwise, it keeps the same value.
How can I do it in MySQL?
Thank you in advance.
An after update trigger should do the trick.
Try:
CREATE TRIGGER `enddate_modif_trigger` BEFORE UPDATE ON `my_table`
FOR EACH ROW
SET NEW.enddate_modif = (SELECT CASE WHEN NEW.enddate > enddate_modif
THEN CURRENT_TIMESTAMP
ELSE NEW.enddate_modif
END
FROM my_table
);
I think you can use an after update trigger
something like this
if(New.enddate >Old.enddate ) then
update yourtable set enddate_modif=NOW() where Your_table_ID=NEW.Your_table_ID;
end iff;
I'm a bit new to PHP as well as MySQL and I'm having an issue (I'm not even sure if this is possible) here's my issue:
I have this table here:
And I use this statement:
SELECT *
FROM (SELECT DATE_FORMAT(STR_TO_DATE(SPLIT_STRING(`date`,' ',1), '%m/%d/%Y'), '%Y-%m-%d') as month
FROM `automation-reports`
) as innerTable
WHERE MONTH(month) = 5
Which gives me:
And I want to be able to get success from that row that contains it here:
I'm not sure if this is even possible considering now data is returned for that row but like I said I'm new to MySQL so I'm not sure of the limitations.
Given that your date column is not a real DATETIME type, you will need to use STR_TO_DATE() but you can use a more complete date string format to return a full DATETIME object from it all at once. The correct format string is '%m/%d/%Y %r', where %r is the 12 hour time hh:mm:ss followed by AM/PM. Using that format, you can wrap the entire output in MONTH() either in SELECT or in WHERE.
SELECT
ID,
reportid,
report,
success,
STR_TO_DATE(date, '%m/%d/%Y %r') AS realdate
FROM
`automation-reports`
-- Apply MONTH() in the HAVING
HAVING MONTH(realdate) = 5
Alternatively instead of HAVING you can put the whole expression in WHERE
...
WHERE MONTH(STR_TO_DATE(date, '%m/%d/%Y %r') = 5)
But really, I would recommend changing that column to a proper DATETIME, as doing so will open up all of MySQL's date processing functions for you and allow the RDBMS to index and optimize the column. You cannot really change the column in place and have MySQL correctly parse the dates. Instead you need to add a new column, fill it, then remove the old column and rename the new (unless you want to keep both).
-- Add a DATETIME
ALTER TABLE `automation-reports` ADD realdate DATETIME;
-- And fill it with dates parsed from your string column
UPDATE `automation-reports` SET realdate = STR_TO_DATE(date, '%m/%d/%Y %r');
-- Drop the old column if you do not need both
ALTER TABLE `automation-reports` DROP date;
-- And rename the new one to the old name
ALTER TABLE `automation-reports` CHANGE realdate date DATETIME;
In this case, you can set the display format of the date after you query it, which is the better course of action than storing the date as a string in the format you want to query it.
If you are in any position to rename this table right now, I would also recommend changing the table name from automation-reports to automation_reports because MySQL does not require the backtick-quoting for the underscore name, while you'll always be needing backticks with the hyphenated name.
So I have a large table that simply cannot be altered without breaking my PHP app. Stupidly (yes I know), I made a start date and end date as VARCHAR with data stored as '03/04/2013' for example.
Now i need to be able to search to see which rows are currently 'active' meaning which rows have a start date before today AND and end date after today.
Is this at all possible with an SQL query?
Action plan to migrate VARCHAR columns to DATE without breaking the application:
Create new indexed DATE columns and fill them with the respective values in the VARCHAR columns:
-- new column
ALTER TABLE MY_TABLE ADD `NEW_DATE_COLUMN` DATE;
-- index
CREATE INDEX `MY_TABLE_NEW_DATE_IDX` ON MY_TABLE(`NEW_DATE_COLUMN`);
-- initial values
UPDATE MY_TABLE
SET `NEW_DATE_COLUMN` = STR_TO_DATE(`VARCHAR_DATE`, '%d/%m/%Y')
WHERE `NEW_DATE_COLUMN` IS NULL;
Create insert / update triggers to cast your VARCHAR columns to DATE and update your new DATE columns with their respective values:
-- triggers
DELIMITER //
CREATE TRIGGER `MY_TABLE_VARCHAR_DATE_BI` BEFORE INSERT ON MY_TABLE
FOR EACH ROW
BEGIN
IF NEW.`NEW_DATE_COLUMN` IS NULL AND NEW.`VARCHAR_DATE` IS NOT NULL THEN
SET NEW.NEW_DATE_COLUMN = STR_TO_DATE(NEW.`VARCHAR_DATE`, '%d/%m/%Y');
END IF;
END;
//
CREATE TRIGGER `MY_TABLE_VARCHAR_DATE_BU` BEFORE UPDATE ON MY_TABLE
FOR EACH ROW
BEGIN
IF NEW.`NEW_DATE_COLUMN` IS NULL AND NEW.`VARCHAR_DATE` IS NOT NULL THEN
SET NEW.NEW_DATE_COLUMN = STR_TO_DATE(NEW.`VARCHAR_DATE`, '%d/%m/%Y');
END IF;
END;
//
DELIMITER;
Use the DATE columns in your queries:
SELECT * FROM MY_TABLE
WHERE `NEW_DATE_COLUMN` BETWEEN
CURRENT_DATE AND DATE_ADD(CURRENT_DATE, INTERVAL 1 DAY);
Take your time and update your application to get ride of places that uses the original VARCHAR columns directly, meanwhile nothing will be broken.
When you are done remove the triggers and the VARCHAR columns:
DROP TRIGGER `MY_TABLE_VARCHAR_DATE_BI`;
DROP TRIGGER `MY_TABLE_VARCHAR_DATE_BU`;
ALTER TABLE MY_TABLE DROP `VARCHAR_DATE`;
Working SQL Fiddle.
Yes you can do that.
Try something like this:-
select date_format(str_to_date('03/04/2013', '%d/%m/%Y'), '%Y%m');
or may be this:-(Just a small change with month and days as I am confused with 03 and 04)
select date_format(str_to_date('03/04/2013', '%m/%d/%Y'), '%Y%m');
OR you may also try to convert your column back to date like this:
UPDATE `table`
SET `column` = str_to_date( `column`, '%d-%m-%Y' );
Use STR_TO_DATE as follows:
WHERE STR_TO_DATE(start, '%d/%m/%Y') < DATE(NOW())
AND STR_TO_DATE(end, '%d/%m/%Y') > DATE_ADD(DATE(NOW()), INTERVAL 1 DAY)
UPDATE `table`
SET `yourColumn` = str_to_date( `yourColumn`, '%d-%m-%Y' );
Convert it do date type without losing your data,add minutes or seconds as needed it.IT will be easier in the long run,but if you prefer dabbling in php..
Or create a new column date type from the varchar one.
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