Cannot get row data after SELECT - mysql

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.

Related

Why doesn't SET and CONVERT work for updating a table in MYSQL

I have the following code, with the column SaleDate in a datetime format, and I am trying to update it in the table to a date datatype instead
UPDATE nashhousing
SET SaleDate = CONVERT(SaleDate, DATE)
and
UPDATE nashhousing
SET SaleDate = CAST(SaleDate AS DATE)
I've tried both cast and convert, but neither modifies the table, does anyone know what's wrong?
You need to ALTER the table structure for that:
ALTER TABLE nashhousing Modify column SaleDate date
This will try to convert the date strings into real dates. Check the data afterwards if it succeeded. If not, you need to change the strings first to the right format. The default date format that always works is YYYY-MM-DD.
Of course if this is live data you should test that with a backup table first.

How to change a column's datatype from text to datetime in mysql?

I'm trying this but it's not working. My original data is formatted like this month/date/year (eg: 2/24/20)
UPDATE Packages SET `date` = STR_TO_DATE(`date`, '%Y-%m-%d %T');
ALTER TABLE Packages MODIFY COLUMN `date` DATETIME;
You should create a new datetime column and populate it using STR_TO_DATE with the text date:
ALTER TABLE Packages ADD COLUMN new_date DATETIME;
UPDATE Packages SET new_date = STR_TO_DATE(date, '%Y-%m-%d %T');
If you'd like, you may also drop the original date column, and then rename the new column:
ALTER TABLE Packages DROP COLUMN date;
ALTER TABLE Packages RENAME COLUMN new_date TO date;
My original data is formatted like this month/date/year (eg: 2/24/20)
Use function with the parsing pattern which matches your current format (the function needs in pattern which should be used for parsing, not the pattern which the data must be converted to):
STR_TO_DATE(varchar_column, '%m/%e/%y')
Look for complete pattern description there.

MySQL: How to set a DATE column by converting all values in a VARCHAR column?

I have a table in which there is a column called "DATE" which contains dates in the format "23-Nov-2017" as datatype VARCHAR. I'm trying to convert this VARCHAR column and store it in a new column called "NEWDATE" of datatype DATE.
I have created the new column "NEWDATE" of type DATE and I am trying to use the STR_TO_DATE() function to perform the conversion. However, I can't get it to work for some reason.
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%m-%Y');
The NEWDATE column is not updated with any values after the statement. I guess this means that the statement does not execute. What am I doing wrong?
EDIT: I have also tried STR_TO_DATE(DATE,'%d-%b-%Y'). However there is still no change to the values in the NEWDATE column
Your format '%d-%m-%Y' does not match your actual date string "23-Nov-2017"
The %m is for numeric month and you have an abbreviated text month
Use %b for 3 char month values like this:
STR_TO_DATE(DATE,'%d-%b-%Y')
EDIT: WorkBench issue
That is just a Workbench config setting to stop you accidentally issuing a HUGE update. You can either turn that setting OFF or frig it a bit by giving it a WHERE clause that will allow it to run like below. Below assumes this table has an id column
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%b-%Y') WHERE id<10000000;
Or
UPDATE table SET NEWDATE = STR_TO_DATE(DATE,'%d-%b-%Y') WHERE id>0;

Format date for mysql insert

Im using LOAD DATA INFILE to import a csv file, the files date format is 29/11/2010 and the database format is 2010-11-29, what can i use to format the date inside the query?
I've tried str_to_date:
SET date_start = STR_TO_DATE(#from_date,'%Y-%m-%d'),
but that only inserts 0000-00-00
MySQL 4.x
LOAD DATA will try to insert your dates as they are. It isn't aware about format and in common case you can not apply some post-processing to your fields (well, different from some format which is allowed inside LOAD DATA syntax itself) - and you can not adjust your values via SET keyword like in MySQL 5.x
Instead you can do following steps:
Declare your table's column as VARCHAR. Let it name be record_date
Do your LOAD DATA query. It will load your dates into record_date column
Add new column to your table, let it be temp_date - with type DATE: ALTER TABLE t ADD temp_date DATE
Update your temp_date column: UPDATE t SET temp_date = STR_TO_DATE(record_date, '%d/%m/%Y')
Drop your VARCHAR date column: ALTER TABLE t DROP record_date
Finally, rename column with correct DATE type to original one: ALTER TABLE t CHANGE temp_date record_date DATE
As result, you'll have your dates loaded into your table as DATE date type. Replace record_date to the name which your original column has.
MySQL 5.x
You can use SET keyword and natively replace procedure, described above. So just do:
LOAD DATA INFILE 'file.csv'
INTO TABLE t
(#date)
SET record_date=STR_TO_DATE(#date, '%d/%m/%Y')
-sample above is for one column and you'll need to add others (if they exist). Date column name is also record_date - so change it to actual name too.
Try something like
update tablename SET date_start = date_format(#from_date,'%Y-%m-%d')
Use DATE_FORMAT() . It will Formats the date value according to the format string. Mysql
date_format(#from_date,'%Y-%m-%d')

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