Excel import to MySQL: Date inserted as 0000-00-00 - mysql

I have used PHPExcel library to import an Excel file (.xlsx) and insert into MySQL. I'm facing two problems with the date field:
1- In the excel sheet I try to change the date format to the desired one. Upon saving, the cells show the format I need (yyyy/mm/dd) but when I double-click to edit them they again change back to how it was before (mm/dd/yyyy).
2- When the file is imported to MySQL through PHPExcel all the columns are inserted correctly except the date column which inserts 0000-00-00. The format in the Excelsheet is the same as in MySQL but it enters is all zeros.
Please help!

The value store in PHPExcel is an Excel serialized datetime value (a float, representing the number of days since 1/1/1900 or 1/1/9004 depending on whether Windows or Mac Calendar was used). Changing the format is simply changing the way this value is displayed.
You will either need to know in advance whether a cell contains a date, or test it using the PHPExcel_Shared_Date::isDateTime() method, and convert it appropriately. You can then either retrieve it using getFormattedValue() to return the value as a formatted string; or use the built-in conversion functions to convert it to a unix timestamp (PHPExcel_Shared_Date::ExcelToPHP()) or a PHP DateTime object (PHPExcel_Shared_Date::ExcelToPHPObject()) that you can then format using the standard PHP date formatting functions before using it in your MySQL INSERT/UPDATE statements.

Change the date format to yyyy-mm-dd, save the file as CSV and import that into your database.

Related

Is there a snowflake command to re-format a timestamp to ensure they are correctly displayed in a CSV export

long time viewer, first time poster.
The issue I am struggling with relates to how my timestamp data appears in excel once I've run my code in snowflake and exported it to CSV. Unfortunately at the moment I am required to double click on these timestamp cells, once in exported into excel, for the true format (how it appears in results in snowflake) to appear.
There is a manual workaround to amend once the data is in excel, however I am automating this report for a client and therefore it must be presented correctly for them prior to the export.
As it stands (see below) the timestamp begins with YYYY-MM-DD and I have been asked to flip this firstly, to begin with DD. I thought that given I need to reformat the current timestamp, I may as well set it up correctly so the timestamp perhaps comes out as text, within the csv export? (from what I have read in other forums if you convert to text it is displayed in excel exactly how it appears in snowflake).
image.png
As you can see, I continue to get the "Cant Parse" error. The example timestamp given, is row 1 of the 'QuoteDate' variable.
The second part of the issue (or really the primary part of the issue) is how the timestamp completely changes format when exported (CSV) to excel. In the screen shot below I have double clicked the first 3 rows, leaving row 4 selected in order to show you all the error - which is the formula bar displays the correct format but the cell itself does not.
image.png
I hope this all makes sense and would love any assistance on how to amend this timestamp issue to run the code correctly, and present my client with a properly formatted timestamp within their extract.
Thank you :)enter image description here
For changing format
SELECT to_char ( to_timestamp ('2019-01-02 09:36:22.507' , 'YYYY-MM-DD HH24:MI:SS.FF') , 'DD-MM-YYYY HH24:MI:SS.FF') AS TS
Timestamp/dates are stored as NUMBER in database, you need to convert in required format for correct display.
to_timestamp --> Converts an input expression into the corresponding timestamp. This expects the input to be in the format provided as second argument.
to_char --> Converts the input expression to a string format.
For preserving data format while converting from csv to excel check Saving to CSV in Excel loses regional date format

What format does a Google Data Studio DATE field expect from the connector?

I am building a Google Data Studio connector with a field called date. The field's type is YEAR_MONTH_DAY. Now, when I return a string, for instance "2020-09-22", the field shows null as a result. What value should my connector provide to have Data Studio recognize it as a date?
"2020-09-22" is of type YEAR-MONTH-DAY, not YEAR_MONTH_DAY
Without knowing the specifics of the connector you are using:
Be careful theat your provided format matches the expected one
Read about supported Dates and times in Data Studio
If necessary, use date formatting methods like e.g. TODATE
If the DATE field you are talking about is the absolute date as mentioned here, the expected format is YYYYMMDD.
For some reason I have had to implement a workaround for this more often lately which is to create a new field in the data source and use the TODATE() function and use that new field in my dimensions on the report.
Try a formula field like:
TODATE(comp_pd_end, "%Y-%m-%d", "%Y%m%d")
The expected date format for the connector is YYYYMMDD, without any separator.
You can check the official documentation:
https://developers.google.com/datastudio/connector/reference?hl=en#semantictype

Format Date field in XAMPP SQL database to german layout (dd.mm.yyy) [duplicate]

I have a table in excel (CSV) which I am importing into phpmyadmin.
the cells show the date fine such as: 09/11/2012 0:00
The cell format is "custom"
when I import the table into phpmyadmin, all the dates turn to: 0000-00-00 00:00:00
What is the correct way to put them in excel so that I can import them into the database.
And how can I convert them to the correct format?
Format your excel dates into the format php accepts. E.g. yyyy-mm-dd h:mm:ss
In excel use the following steps:
Right click the column heading that contains the dates.
Select Format Cells
Click the "Custom" category
Paste "yyyy-mm-dd h:mm:ss" in the input box.
*. Save the document.
In php SQL query, make sure to set this column to date in anycase if php side is going to treat the date as a string.
You may use : ‘STR_TO_DATE(#date, '%Y-%c-%e %H:%i:%S')‘
I had this problem and thought I formatted them to format YYYY-MM-DD. They were displayed that way, but in the Formula bar it was still in DD/MM/YYYY format.
So I actively changed the date with:
=YEAR(A1)&"-"&IF(MONTH(A1)<10;"0";"")&MONTH(A1)&"-"&IF(DAY(A1)<10;"0";"")&DAY(A1)
And then copied the values of these cells to replace the original ones...

Import CSV to SQL - Date format issues: Zeroes

I'm trying to import CSV data into MySQL database. I have four Date fields. when the import finishes, all my dates are zeroes: .
I've set the date format to DATE in Excel prior to saving it as CSV. I've set the table field as DATE in MySQL.
I'm using phpMyAdmin to do the import.
Mysql only excepts dates in YYYY-MM-DD format.
The Date format in excel is 06/09/2015. Thus, not in the format that mysql is expecting. You must change this to a format that is accepted in mysql otherwise all your date fields will appear as 0000-00-00.
In order to change the date format in excel: right click on the top cell. Choose format cells from the drop down list. change the local to something like 'Afrikans'. Choose the format that looks like 2001-03-14. Use the top cell to fill down. Then save the document.
Just a quick note: Excel sometimes tries to do too much and will revert this column back to a the English(U.S) default time zone. So, if you plan on doing more editing make sure that the column has not reverted back.
Here is a link to more string literals on dev.mysql.

using excel dates for importing into phpmyadmin sql

I have a table in excel (CSV) which I am importing into phpmyadmin.
the cells show the date fine such as: 09/11/2012 0:00
The cell format is "custom"
when I import the table into phpmyadmin, all the dates turn to: 0000-00-00 00:00:00
What is the correct way to put them in excel so that I can import them into the database.
And how can I convert them to the correct format?
Format your excel dates into the format php accepts. E.g. yyyy-mm-dd h:mm:ss
In excel use the following steps:
Right click the column heading that contains the dates.
Select Format Cells
Click the "Custom" category
Paste "yyyy-mm-dd h:mm:ss" in the input box.
*. Save the document.
In php SQL query, make sure to set this column to date in anycase if php side is going to treat the date as a string.
You may use : ‘STR_TO_DATE(#date, '%Y-%c-%e %H:%i:%S')‘
I had this problem and thought I formatted them to format YYYY-MM-DD. They were displayed that way, but in the Formula bar it was still in DD/MM/YYYY format.
So I actively changed the date with:
=YEAR(A1)&"-"&IF(MONTH(A1)<10;"0";"")&MONTH(A1)&"-"&IF(DAY(A1)<10;"0";"")&DAY(A1)
And then copied the values of these cells to replace the original ones...