MySQL date format is not supported - mysql

When we call this procedure
Call get_reports_by_time('2014-12-22 11:19:26 AM').
no data is returned
but when we call this
Call get_reports_by_time('2014-12-22 11:19:26 ')
all the records for that day is returned. why its so?

string representation of dates/timestamps,... is dependent on your db settings.
In your case one representation can be converted to a date (assuming that you work with a date type in your stored procedure) and one representation cannot be converted to a date.
I higly suggest to use date and time types and not any string representation for your paremeters (if you need a string representation then convert it by YOURSELF using date/str_to_date)

Related

MySQL equivalent of php's strtotime()

Is there a way to convert a string such as "-1 week" or "-5 minutes" into a datetime value in MySQL similar to php's extremely convenient strtotime() function?
I have a table that stores a human-readable time interval (such as "2 minutes") in one column and a datetime in another column.
I would like to select the rows where more than the amount of time specified in interval has elapsed since datetime.
MySQL doesn't have an equivalent of PHP's strtotime() in the sense that there is nothing that will automatically attempt to parse and determine the format of a date string using by assuming multiple formats.
What it does have is STR_TO_DATE(str,format) which requires you specify the format of your date, time or date + time string. It is the equivalent of PHP's date_create_from_format(format, str) function (though the format of the format parameter are different).
Here are some examples given from the MySQL documentation. They show a date being passed along with the format string that lets it know how the date string is to be interpreted:
SELECT STR_TO_DATE('01,5,2013','%d,%m,%Y');
SELECT STR_TO_DATE('May 1, 2013','%M %d,%Y');
Alternatively, you can cast a string to a date, time or datetime type, but they require a specific format (YYYY-MM-DD hh:mm:ss.fraction) for it to work:
SELECT CAST("2019-11-21" AS DATE);
If you deviate too far from that format it will make a few assumptions but could produce an incorrect date.

Parsing Custom Date Format in MySQL

Problem
I need to query out a date value for use in some ETL processing. However, the data is stored in MySQL as a VARCHAR column in values like 1/1/19.
What I've tried
A simple CAST(myDateColumn as DATE) fails as I get values like 0001-01-19 returned back.
Question
Am I able to pass a custom date format string into the CAST call somehow to tell it how to parse out the date parts? If not, could a SUBSTRING type of function help here?
As discussed in the comments above, you can use the STR_TO_DATE() function to parse a string into a proper YYYY-MM-DD date.

Expected a long or a date string representation of a timestamp value Cassandra

I'm trying to insert a row in a Cassandra table using the query below
INSERT INTO battery_time_series_by_producer JSON '{"timestamp":1514413581,"customer":"com.fetchcore-cloud.local","stream":"/sw8/sensor/battery","producer":"freight18","data":{"battery_level":1,"is_charging":true,"timestamp":1514413581}}';
and I get the following error
Expected a long or a datestring representation of a timestamp value, but got an Integer: 1514413581
The field timestamp in the table is indeed of the timestamp format. However, how can I convert this to a long given that this is just JSON
My version of Cassandra is 3.11.1
Cassandra will try to parse json object to closest data type possible, e.g. the value 1514413581 is converted to Integer type. If you are giving a larger number, it will be parsed as Long.
Kindly note that that timestamp type is representing a number of milliseconds since the standard base time known as the epoch. The values in your example represent the time in year 1970.
In short, it's either to use string format (e.g. "1514413581") or pass recent timestamp value (which automatically convert to Long later) in your cql statement. Hope it helps.

Wrong MYSQL (date) output

i have a database that contains over 2000 records and the date is wrongly formatted (mm/dd/yyy). I need to change this with mysql into dd/mm/yyy.
i have this code:
UPDATE wp_team_workshop_availbility SET available_date = DATE_FORMAT('available_date', '%d-%c-%y')
but all i creates is an empty field.
If you are storing it as a date (datatype), it is just a date - there is no format. However, you also don't want available_date as a quoted string, which is trying to convert the string "available_date" into a date.
My guess is you have the date stored as a string (you really shouldn't). However, what you will want is something more like:
UPDATE wp_team_workshop_availbility
SET available_date = DATE_FORMAT(STR_TO_DATE(available_date,'%c/%d/%Y'), '%d-%c-%y');
i.e. you need to convert the string to a date and then convert it back to a string.
But really, you should take advantage of this opportunity to change your storage so you are using the right datatype.

Convert varchar string data to time format in mysql

I need to convert varchar string data to time format in mysql.
For example,
I have a varchar column in table which stores time. The accepted values should be like 9:30 AM or 1200 PM. But currently it has either blank values or it has values like 9.30am or 12:00
There are many records like this, so cannot update manually.
Ithere any work around or function or procedure to do so?
please help.
Thanks
You can use the STR_TO_DATE() MySQL function to convert any string to a date.
Additionally you can use TIME() to extract the time portion of a datetime. A combination of both function is used to convert an arbitrary date string to a datetime and then you can extract the time portion from it as a valid MySQL TIME.
By default MySQL functions follow standard format but custom format can be specified and if your values don't use the international formats you'll need to check with the documentation and provide the format your system is using.