Automatic conversion MySQL datetime to milliseconds on Apollo client? - mysql

I am using Apollo server and Apollo React client. The server runs a query against the MySQL database. In GraphQL schema I have defined the corresponding field sampleTime as String in the type Session. (There is no Date type in GraphQL.) But apparently some kind of default conversion is made on the way between the Apollo server and the result of useQuery. The field sampleTime shows the value as a number and not as a string. The number is apparently the JavaScript Date object.
In this case I don't need such a conversion. I found a solution on the server side by converting the SELECT output to the formatted string.
But why this conversion does occur at all?
Thanks

Related

Ensuring Same date format in JSON objects

Setup :
Angular 8 + Spring boot 2.2.3 + (Oracle 12.1 / PostgreSQL 12.3)
We are building a approval System where User can fill online form like google forms and can submit for approval. Rather than normalizing form structure , we'll be storing metadata in JSON format in our DB.
Values that are filled in form would also be going as JSON format in DB.
One point come up as a concern , in DB we can store date in particular format like 12-May-2020 which would be consistent across all inserted data as this data might be used to construct reports in near future.
Based on pros/cons of this approach need to decide on DB / data model as well.
So,
Is there any way I can enforce date format in JSON
If this cannot be done in JSON , what options do i have at Angular 8 / Spring boot application level which would enforce all developers / date components / date fields to use same date format.
If these cannot be done , how can I handle different formats in Query over JSON data that would be used in reporting or otherwise , both in Oracle and PostgreSQL
Regards
The proper solution to your problem is to create a real, normalized date column.
If for some reason you can't or don't want to do that, I would create a check constraint that validates the date format by trying to cast it to a real date value.
The following is for Postgres, but you can create something similar for Oracle as well:
create table the_table
(
form_data jsonb,
constraint check_valid_date check ( (form_data ->> 'entry_date')::date is not null)
);
Obviously you will need to adjust the expression that gets the date value from the JSON to match the key and path inside your json value.
The cast to date will require that the date value is entered using the ISO standard format, yyyy-mm-dd which is the only "consistent" way to store a date as a string.
Alternatively you can use to_date() with a format mask:
check ( to_date(form_data ->> 'entry_date', 'yyyy-mm-dd) is not null)
in DB we can store date in particular format like 12-May-2020
You are mistaken, Oracle doesn't store date in that format. It is internally stored in TYPE12/13 data type. Each bit represents different parts of the date. What you see is a human readable format displayed according to your locale-specific NLS settings or using TO_CHAR with format mask.
To keep it aligned across all platforms, use the globally accepted ANSI standard date literal which uses a fixed format 'YYYY-MM-DD'.
For example:
DATE '2020-05-21'

SSRS issue matching date with parameters

I have a SSRS report using an ODBC connection with MySQL. I have a problem to match the date with the parameters. When I type a date manually in the query it works fine, no problem. But when I use the parameters, it doesn't return anything. One thing I found is that the date data returned is under the format YYYY-MM-DD and the date from parameter is DD/MM/YYYY. I tried CONVERT and STRING_TO_DATE functions of MySQL but no luck. Any clue?
convert the parameter value to a date based on the format sent from the SSRS to match the format you are using in the manual test.
Converting a date in MySQL from string field

Map String date value of a Csv to a column in mySql database using Informatica Cloud

I am doing Data Integration project to create a Data Warehouse in mySql. I have a couple of Csv/flat files to be ingested into Informatica Cloud which will populate the destination table in mySql database (The destination table is already created).
The Csv files have columns related to datetime but they are in String format (MM/DD/YYYY HH:MM) and I am not able to pass them through Informatica Cloud to the mySql datatbase tables. The column in mySql Database has format YYYY-MM-DD HH:MM:SS as the data type is datetime.
I tried different strategies of keeping the destination columns datatype as datetime and sometime varchar.
I got Following errors on different attempts with different changes.
*Transformation [Expr_DSS_0010LY0I000000000002_1] had an error evaluating output column [SystemModstamp_OUT]. Error message is [<<Expression Error>> [TO_DATE]: invalid string for converting to Date... t:TO_DATE(u:TO_CHAR(t:TO_DATE(u:'5/30/2013 12:26',u:'mm/dd/yyyy hh:mi'),u:'yyyy-mm-dd hh:mm'),u:'MM/DD/YYYY HH24:MI:SS')].
Transformation [Expr_DSS_0010LY0I000000000002_1] had an error evaluating output column [CreatedDate_OUT]. Error message is [<> [TO_DATE]: invalid string for converting to Date... t:TO_DATE(u:'6/24/2008 18:23',u:'mm/dd/yyyy hh24:mi:ss')].
Transformation [Expr_DSS_0010LY0I000000000002_1] had an error evaluating output column [CreatedDate_OUT]. Error message is [<> [TO_DATE]: invalid string for converting to Date... t:TO_DATE(u:TO_CHAR(t:TO_DATE(u:'6/24/2008 18:23',u:'mm/dd/yyyy hh24:mi'),u:'YYYY-MM-DD hh:mm:ss'),u:'MM/DD/YYYY HH24:MI')].*
I also tried the documentation related to Informatica Cloud (Csv datetime) but didn't get any concrete solution.
I will be really glad if someone can help.
Thanks in advance.
A single TO_DATE should work
TO_DATE(input_date_field,'mm/dd/yyyy hh24:mi')
I just finished solving the exactly same issue myself.
There is no good way to do this, I will explain what I did:
Basically I treated the date columns as string in power center. i.e.
I changed the target definition only in PC to varchar for all the dates column.
SQ, expects everything is string for their attributes.
Where as the physical definition is still datetime in mysql DB.
For me this worked. hope it works for you too. Good luck !!

Why my date is written 0000-00-00

I have a table where the date column name is defined as proddate and defined as DATE.
Here is the code I write the value into table:
cmd.Parameters.Add("#Mydate", MySqlDbType.DateTime).Value = Convert.ToDateTime(MyArray[4]).ToString();
This gives a result of 0000-00-00
When I change it to
cmd.Parameters.Add("#Mydate", MySqlDbType.DateTime).Value = Convert.ToDateTime(MyArray[4]);
The result is correct. eg: 2013-11-14
However few lines down I have this code
cmd1.Parameters.Add("#date", MySqlDbType.DateTime).Value = Convert.ToDateTime(MyArray[4].ToString());
This gives no error. I get the correct result in table
And few lines after I have this code in the same method:
cmd3.Parameters.Add("#Mydate", MySqlDbType.DateTime).Value = MyArray[4].ToString();
This gives no error too
The last two lines I did the mistake for testing. But the columns of 2 last exemples are defined as DATE format
Any idea ? Or Am I welcome in the mystery world of Mysql ?
PS: I use MYSQL CONNECTOR 6.7.4.0 .NET3.5 and C# 2008 Express. In my server MYSQL is 5.0
In the first version, you're converting your value to a DateTime and then to a string, using the default format for a DateTime value. MySQL is then trying to parse that, and presumably failing.
In the second version, you're parsing your value as a DateTime, and supplying it to MySQL directly. This is the best approach, although you need to be careful about the input format you're using.
The third version is like the second, just with an explicit ToString call. If MyArray is of type string[], that's basically a no-op.
The fourth version is providing the string input directly to MySQL, which is presumably able to parse it itself. I would suggest avoiding that form though - parse it in the .NET code, where you have much more control.

Conversion failed when converting datetime from character string on reporting service

when i convert or cast string to date ran succesfull my query from t-sql on sql server but when i assign dataset query on reporting service,i take this error.
'Conversion failed when converting datetime from character string.'
It will depend on the regional settings on your server. I guess your reporting services server thinks days and months are in the opposite position from your SQL server.
The easiest way to consistently get dates to convert from string properly without having to consider regional settings is to use military date format: YYYY-MM-DD
Military format always converts correctly.
Make sure you are not forgetting to put the braces on the "parameter value" field when editing parameters on the DataSet Properties window:
This gives the conversion error:
Parameter Name Parameter Value
#queryDateParameter #reportDateParameter
Correct is:
Parameter Name Parameter Value
#queryDateParameter [#reportDateParameter]
I've had the same issue when trying to pass parameters through SSRS.
when the parameter types are text, SSRS does a conversion using server region settings, in which case, a MM/DD/YYYY and DD/MM/YYYY difference resulted in this error.
Solution for me was to change the parameter type to datetime. This enforces the conversion to be handled correctly.