php/mysql routine to help me extract all dates strings stored in a column as text dd/mm/yyyy into another fresh column with mysql date datatype - str-to-date

i am new here and i need help with writing a php/mysql routine to help me extract all dates strings stored in a column as text in this format dd/mm/yyyy into another fresh column with mysql date datatype in the format yyyy-mm-dd.
i have tried to query through the table using a date range and it seems not to work, so i have resolve to change th already store dates into the proper mysql format to enable me query with date easily.
indeed i am most greatful of any assistance i get.
thanks
Handyx

First of all you probably shouldn't be doing what your doing, convert the date on the go. Nevertheless here's the SQL query to do what you want to do
USE `DATABASE`;
UPDATE `TABLE_NAME` SET `TABLE_NAME.NEW_COLUMN` = DATE_FORMAT(
STR_TO_DATE(`TABLE_NAME.OLD_COLUMN`,'%d/%m/%Y'), '%Y-%m-%d');

Related

how to enter data into mysql table with wrong date format

I have a CSV file with lots of different columns, with one column having date which is in DD-MM-YYYY format . now mysql's default format is YYYY-MM-DD . I was planning to load my CSV file directly into my mysql table , but this may cause a problem. what should I do . PS- i am not planning to run a code from some other language on the file , so i would appreciate solutions that include the use of mysql itself.
If you are able to perform multiple steps you could first import the csv into an intermediate table which has the same properties as your real data table, except for the date columns.
Then you can just insert it into the real table and format the date-string while selecting
INSERT INTO realTable (/* ... */)
SELECT /*.... */, STR_TO_DATE('01-5-2013','%d-%m-%Y')
FROM intermediateTable;
When ready truncate the table, done. Should be done in a transaction so you can rollback if something goes wrong.
edit:
Seems to be a better way: https://stackoverflow.com/a/4963561/3595565 So you should be able to format the date while importing by using a variable
You can do two things.
Since your column format is DD-MM-YYYY directly you can't convert YYYY-MM-DD.You can use substr to get DD, MM, YYYY seperately and insert in your table.
Otherwise you can store as varchar not date type.

How can I change the date time format in my query

I am running a Query to select files with a certain date range at the end of the file name. WHat i have looks for date format YYYMMDD, But I need it to check for MMDDYYY. Can anyone give advice on how to format?
MySQL through MySQL Workbench.
LOAD DATA LOW_PRIORITY LOCAL INFILE 'C:\\Talend\\importall'+CONVERT(VARCHAR(8), GETDATE(), 112)+'.csv' INTO TABLE `production`.`pendi_complete`
Check out the DATE_FORMAT and the STR_TO_DATE functions, it should do the trick.

Change date format in mySql database table

Morning everyone. Just a quick question. In my existing MySql database I've few columns with dates in it. Currently these are yyyy-mm-dd format but now I need to change it all to dd-mm-yyyy format. I've tried select date_format(curdate(), '%d/%m/%Y'); but it doesnt update existing data in my table.Is there any easy way of doing it? Or even if its not easy could you give me some suggestion how to do it please and thank you.
Try using
SELECT DATE_FORMAT(dateColumn,'%d/%m/%Y') AS dateColumn FROM table
You cann't able to change the default date format in table. Default DATE format is 'YYYY-MM-DD'. But you can able to retrive the date column value as your way.
Ref: http://dev.mysql.com/doc/refman/5.1/en/datetime.html

How should date be formated for MySQL WHERE clause

I have a table which contains a DATETIME column. I wish to include a WHERE clause to limit records based on this column. The format of the constraint is currently 08/23/2012. If need be, I can convert the current format to whatever is needed using PHP.
Should I use PHP to convert the format before passing it to MySQL (and if so, what format should it be), or should I pass it this way, and use MySQL to convert it (and again, how?)
Thanks
You should convert it in PHP:
$date=date( 'Y-m-d H:i:s', strtotime("08/23/2012")) represents the DATETIME format in MySQL
Check the docs: http://dev.mysql.com/doc/refman/5.1/en/datetime.html
I don't know if there's official guidance but I find yyyy-mm-dd to be reliable when using code to format a date for a MySQL query.

Problem with Date field after migration from mysql to oracle

I have lately migrate my JIRA database from mysql to oracle,
my problem is the field "created" exists in the jiraissue and changegroup tables,on this field I effectuate many calculation but I was surprised by the difference of the format of the fields.
in mysql database the field creation has the type timeStamp so it has the follwing format:
and in Oracle database it has the type date and the format like the following:
How can I resolve this problem?
The format of your displayed Oracle DATE column is due to your IDE that you are viewing it through.
Oracle stores all portions of a date, to display the full date stored use this:
SELECT TO_CHAR(created, 'DD-MON-YYYY HH24:MI:SS')
FROM jiraissue;
This will show you the full date that has been stored including the time portion. To store timestamps you need the column to be designated as a timestamp datatype column.
Ollie.
EDIT: You could change the NLS Date Format of your IDE to always show the full date format in it's settings somewhere.
Dates are not held as formatted text in Oracle. What you are seeing is a tool's (Toad's?) default formatting of the date to display it to you. This can be changed via a preference somewhere. It could be that the time component has been lost in migration, but that is unlikely. Try running this SQL to see:
select to_char (created, 'YYYY-MM-DD HH24:MI:SS') from jiraissue;
That should show the dates just as they appeared in MySQL.