MySQL date and datetime formats unrecognised by Data Studio - mysql

I've linked Google data studio with a MySQL database using the standard connector. Almost everything works fine except for date and datetime fields.
I have these 2 fields in phpmyadmin (field name, field type, output):
Validated_date datetime 2017-09-27 12:31:04
Expiration_date date 2017-12-24
In Data Studio I've set these types, but none of them are recognised:
Validated_date Date Hour (YYYYMMDDHH)
Expiration_date Date (YYYYMMDD)
I tried to format the field with date_format in my SELECT:
DATE_FORMAT(p.Expiration_date, '%Y%m%d') AS "Expiration Date"
I even tried other date_formats but they're never recognised as dates in Data Studio:
DATE_FORMAT(p.Expiration_date, '%Y/%m/%d') AS "Expiration Date"
DATE_FORMAT(p.Expiration_date, '%Y-%m-%d') AS "Expiration Date"
Any idea?

I had the same issue. My approach to solve this is to modify the date format within Google Data Studio by creating a new dimension, reformatting the mySQL Datetime into the desired format.
In your example you use DATE_FORMAT. I wonder if you apply this in Data Studio (which does not know DATE_FORMAT) or in mySQL?. If you do it in data studio and leave your mySQL/phpmyadmin untouched you can use this:
TODATE(your-date-field, 'DEFAULT_DASH', '%Y%m%d')
This will take the date format YYYY-MM-DD with optional time in HH:ii:ss and reformat it into YYYYMMDD which works with data studio.

I've never tried directly with Data Studio but when I extract datetime fields from mySQL to use in BigQuery I use:
CONVERT(DATETIME2(0), [DATETIME_FIELD])
This removed any milliseconds which generally cause problems and convert it to a recognisable datetime format for Google

I have used the follow formule and working for me:
TODATE(yor-field-from-mysql, '%Y-%m-%dT%H:%M:%S', "%Y%m%d%H%M")Then I choose ***YYYYMMDDhhmm*** format in DataStudio. I hope it helps.
Edit: In this case, date came from javascript using moment.js

I had the same issue. For me, the only solution was create a formula this way:
CONCAT(SUBSTR(field, 1, 4), SUBSTR(field, 6, 2), SUBSTR(field, 9, 2))
and use this as AAAAMMDD format

Related

How to get Time from Mysql Date Format DD/MM/YYYY?

I want to get time from mysql dd/mm/YYYY H:M:S format.
I have tried,
SUBSTRING_INDEX(field, 'delimiter', index)
but am looking for a better solution.
have tried, DATE_FORMAT(field, "%H:%i:%s") but it returns NULL because my date format was not native (YYYY-mm-dd)
it was 02/05/2019 19:38:27
How to get time from this above format in a better way?
NOTE: I am storing date like above.. this fetching form SQL Server
I guess you can first use STR_TO_DATE followed by CAST(... AS time). Casting instead of formatting allows you to use the result in date/time calculations.
SELECT CAST(STR_TO_DATE('02/05/2019 19:38:27', "%d/%m/%Y %H:%i:%s") AS TIME)
Ideally you should teach SQL Server to export dates in yyyy-MM-dd hh:mm:ss format.
This is how i Resolved,
TIME(STR_TO_DATE(d.in_punch, "%d/%m/%Y %H:%i:%s"))
also as per #Salman A
CAST(STR_TO_DATE('02/05/2019 19:38:27', "%d/%m/%Y %H:%i:%s") AS TIME)
this also worked.

Importing a datetime field from csv/txt into Access

I have the output of a SQL Server 2008 query saved to a text file and I am trying to link it to an Access database (Access 2007-2010).
The text file contains two date columns in the SQL Server 2008 datetime format, like this:
EFFECTIVE START DATE:
---------------------
2013-07-01 00:00:00.000
EFFECTIVE END DATE:
-------------------
2014-06-30 00:00:00.000
In the Import wizard, I click Advanced and change the following:
Code Page = Western European (Window) instead of Unicode (UTF-8)
For the 2 datetime columns:
data type = Date/Time
date order = YMT
Date delimiter = -
Leading zeros in dates = ticked
While still in the import wizard the data looks fine but as soon as the import is finished and I open the table in Access, I see #Num! in the two date columns.
I have tried a combination of settings in the import wizard (leave the code page as Unicode, not change the date order, not ticking the leading zeros in dates, etc) but in the end I had to import the dates as text, which stops me from doing any calculations now.
Many thanks in advance for the help
in the end I had to import the dates as text, which stops me from doing any calculations now.
True, you can't manipulate the text fields as true dates, but you can run a make-table query to convert them to real Date/Time values like so:
SELECT
ID,
CDate(Left([Field1],InStr([Field1],".")-1)) AS Date1,
CDate(Left([Field2],InStr([Field1],".")-1)) AS Date2
INTO SqlDataWithRealDates
FROM SqlData;
The issue with the original import is that Access Date/Time values do not support fractional seconds so Access will never recognize the values as such if they include 00:00:00.000. The above query removes the trailing .000 before passing the strings to CDate().
Another alternative as suggested by Johnny Bones in the comments below is to alter the SQL Server query to use something like CONVERT(VARCHAR(10), [Field1], 101) AS Date1 to remove the time component from the strings that will eventually be imported.
I'm just adding this as an answer, since it ended up being the one that worked:
If you have control over the output from SQL Server, you can change the format of the date field in your view/stored procedure. Something like
SELECT CONVERT(VARCHAR(10), [Field1], 101) AS Date1
That will strip the time off it, and Access should recognize it.
I found if you use "M/d/yyyy HH:mm:ss" as the date format (.NET), Access will import the date/time correctly, at least for if you are in the United States. Note, the hour is the 24 hour time. It won't work in 12 hour time with the AM/PM indicator. This is different than Excel, which does work with the 12 hour time. If you include milliseconds, it won't work. You can leave the time off if you don't need it.
If your output is CSV file, change the output format to "MM/DD/YYYY", and drop the time from the date.
It will import just fine.
If you need to use the time, make another column for it, and strip out the date leaving only the time.
I was able to use hundreds of hours with two places.
Access does import correctly the date and time information in the data. You need not drop (or remove) the time information. I have not tested for fractional seconds. However, if you have duplicates in the data column you may need to fix it and also you should correctly set the date format (YMD, DMY, etc.)
Here is a recent link to a correctly imported text file.
http://hodentekmsss.blogspot.com/2017/02/importing-text-file-into-ms-access.html
I had faced the same issue while importing CSV file with datetime. What I had done to solve this issue was by splitting date and time and merge.
Convert(varchar, yourdatecolum, 101 ) + ' ' + Convert(varchar, yourdatecolumn, 108)

How to use MYSQL on dates formatted as MM/dd/yy

I have a database full of dates that are in the format: MM/dd/yy. For example, today's date (November 4, 2011) is saved in the database as: 11/04/11. I'm having trouble with date ranges since my dates aren't formatted as yyyy/MM/dd (example: 2011/11/04). I don't have a way to change the way the database is populated, so I need to account for the date formatting differences within my MYSQL queries (via VB.NET). Here is my query (it doesn't work well because the dates are in the incorrect format):
SELECT CMP_DATE FROM my_data WHERE OBJ_DATE >= '1994/01/01' AND CMP_DATE <= '2011/11/04'
Is there anyway to reformat the dates within the query? Thank you.
The function you need is STR_TO_DATE:
SELECT CMP_DATE
FROM my_data
WHERE STR_TO_DATE(OBJ_DATE, '%d/%m/%y') >= '1994-01-01'
AND STR_TO_DATE(CMP_DATE, '%d/%m/%y') <= '2011-11-04'
#Piskvor has a good point, too, you should consider interpreting the 2-digit year yourself, instead of letting mysql perform some arbitrary conversion.

How to make the default date format dd/mm/yyyy in SQL Server 2008?

I have an Excel file that contains a column full of dates in the dd/mm/yyyy format. When I try to import it using openrowset, it said that there was a datatype mismatch. I have a table where the date is defined as type date. Now, I know that the default date format in SQL Server is yyyy-mm-dd. How can I avoid this conflict? Is there a way I can make the default date type be dd/mm/yyyy? I need to do this import operation everyday and it has to be automated and so I cannot afford it to fail in between. I tried using sp_addlanguage to make it British as the default date type is dd/mm/yyyy there, but it didn't work :(. I'm using SQL Server 2008 and Windows 7, if that is of any help. Please help me out! Thanks!
You could CONVERT the incoming data before you insert it. So, in the openrowset statement, where you select the field, you could surround it with a CONVERT statement. Here's an example:
print convert(date,'19/07/2010',103)
This is a UK style date, but if you run it you can see that it's converted it to SQL-friendly format.

SQL Server 2008 VarChar To DateTime

I have a table where unfortunately a number of dates are stored as strings.
I have a number of reports that cast them to datetimes and filter by them. This was working fine until today when all of a sudden i'm getting this error
"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."
The dates are all stored in the format of "yyyy-mm-dd" and are all valid.
If I run the following SQL statement
SELECT CAST('2010-06-02' AS DateTime)
I would expect to get "2010-06-02" however as of today I'm getting "2010-02-06" something has changed with the way SQL formats dates. I've had a look in regional settings on the server and it all looks to be correct.
What else could be causing this?
Try setting the format explicitly
select convert(datetime, '2010-06-02',101)
An unambiguous way of getting this conversion is to do the following:
SELECT CAST(replace('2010-06-02', '-', '') AS DateTime)
And that will always be interpreted as YYYYMMDD, ignoring the set dateformat ydm declaration or any cultural settings that the database has.
Q1: What else could be causing this?
The local, You probably are under the local101(US) and put data from 103 (British/French)
Like barry sad use convert