MySQL datetime vs timestamp and highcharts - mysql

I have been using highcharts for datetime-based charts and it needs a timestamp to make the chart.
In my DB I have a DATETIME column and I convert to timestamp when I do the select:
SELECT UNIX_TIMESTAMP(CONVERT_TZ(`datetime`, '+00:00', ##session.time_zone)) * 1000 AS datetime
I have a lot of rows and I think that select may be slow. Should I store the dates as timestamp instead of datetime?

Related

In mySql how to ignore past time when i compare any present or any datetime with any column having datetime?

Sql query to ignore past time and show records when i compare with present datetime with datetime column.
My query:
select * from appoinments WHERE (date(app.start_time) = date(NOW()))
start_time is a column having date and time both in format 'Y-m-d H:i:s'
i need to exclude past time from this query.
Because i'm getting extra records because of this

MySQL convert timestamp and time string to DateTime format

In my table have two columns, one as timestamp to save the date and one as time string to save the time with period.
Eg:
I want to combine them into one column as DateTime format in the result then order by desc on that column.
Here is the example: http://sqlfiddle.com/#!9/25eb21/4
The column name 'Datetime' expected is Datetime or timestamps type, so I can sort correctly on it.
You do not need to convert the values to integers to add them. MySQL has built-in functions for this purpose:
SELECT *,
addtime(apptDate, str_to_date(apptTime, '%h:%i %p')) as datetime
FROM appt
ORDER BY Datetime DESC;
If apptTime is just a time value (which it should be), then you obviously do not need to convert from a string. I would usuggest fixing the data model.
Let me assume that you want to add the duration that is stored as a string in column apptTime to timestamp in column apptDate.
A typical approach uses str_to_date() to turn the string to a datetime, then converts the time portion to seconds using time_to_sec(), which we can then add to the timestamp using date artihmetics.
So
select t.*
apptdate
+ interval time_to_sec(str_to_date(appttime, '%h:%i %p')) second
as newapptdate
from mytable
select addtime(appDate, appTime) from ...
Your appDate contains a time, probably because you are applying a timezone. Either convert your two columns to the timezone your data is supposed to be in with convert_tz(), or extract the date part of it with date(appDate) before you add it. It wasn't clear which of the columns was a string, but extract() or str_to_date() is the way to parse a text into a date and/or time.

Cannot scan the right values of Order date in sql

I want to fetch all datas that corresponds in the chosen date range.
So the problem is that. When theres included time in the data. It can't fetch the required data to be displayed. But when I remove the time on it. It displays really well. What can I do to make it right?
EXAMPLE VALUES:
2018-10-29 01:21:29pm
2018-10-30 01:21:29pm
EXAMPLE VALUES THAT WORKS:
2018-10-29
2018-10-30
My query:
`"SELECT *,SUBSTRING(order_date,1,10) from orders where order_date >='$fromdate' AND order_date <='$todate'"`
Ideal Solution: You will need to change the datatype of order_date from Varchar(500) to Datetime type, using Alter Table command.
Now, it is noteworthy that the MySQL datetime value is in YYYY-MM-DD HH:MM:SS format. So firstly, you will need to change your datetime string to MySQL datetime format string. Otherwise, directly changing the datatype will lead to irreparable loss/truncation of data.
Your datetime value 2018-10-29 01:21:29pm is basically of YYYY-MM-DD HH:MM:SS AM/PM (12 hour format). In terms of format specifiers, it would be: '%Y-%m-%d %h:%i:%s%p'. Complete list of available format specifiers can be seen in MySQL docs.
Firstly, we use Str_To_Date() function to convert all your data into proper Datetime format.
UPDATE orders
SET order_date = STR_TO_DATE(order_date, '%Y-%m-%d %h:%i:%s%p');
Now, next step is simple. Just modify the datatype to datetime:
ALTER TABLE orders
MODIFY COLUMN order_date datetime;

Want to run a query which gives me results between two dates. I have timestamp in unix epoch format

I have got timestamps in epoch UNIX format. I want to run a query by directly giving date and not timestamp. How is that possible?
SELECT FROM_UNIXTIME(timestamp)
FROM report_data
WHERE timestamp = '1399376713'
I used this to convert to human readable format.
My database is something like this
timestamp event_type flags
1399357862 701 null
I want to give a particular date in my query and get the result.
It's possible using the FROM_UNIXTIME function.
This assumes that your table contains columns in DATETIME or TIMESTAMP, and you are wanting to supply 32-bit integer values in the query.
For example:
SELECT ...
FROM mytable t
WHERE t.datetime_col >= FROM_UNIXTIME( ? )
AND t.datetime_col < FROM_UNIXTIME( ? )
The integer values supplied as arguments to the FROM_UNIXTIME function will be interpreted as unix-style "seconds since epoch" integer values, and be converted to a DATETIME value using the current timezone setting of the client connection.
This approach will enable MySQL to use a range scan operation using an index with a leading column of datetime_col.
What's not at all clear is what the datatype of your column is, and what values you want to supply in the query. If the columns is datatype DATE, DATETIME or TIMESTAMP (which would be the normative pattern for storing date/time data), then you can specify date literals in standard MySQL format, 'YYYY-MM-DD HH:MM:SS'.
WHERE t.timestamp_col >= '2015-02-11 07:00'
AND t.timestamp_col < '2015-02-11 23:30:00'
If you are storing the "timestamp" as an integer value, then you will need the right side of the predicates to return an integer value, e.g.
WHERE t.integer_col >= UNIX_TIMESTAMP('2015-02-10')
AND t.integer_col < UNIX_TIMESTAMP('2015-02-10' + INTERVAL 24 HOUR)

Convert a MySQL timestamp to actual datetime format ina MySQL query

I have following query and data saved in a MySQL database with a timestamp column. I have to display the timestamp as the actual data and time format like 1333007294 it should be displayed as 03-MAR-2012 12:48:14 PM. I have tried to use MySQL using from_unixtime but it displays the datetime like this 3/29/2012 12:00:00 am. I need to get the format like 03-MAR-2012 12:48:14 PM here is my query.
SELECT DATE( FROM_UNIXTIME( `timestamp` ) ) AS timestamp
from vlasteventwdevice
order by 1 desc
Try like this:
FROM_UNIXTIME(`timestamp`, '%d-%b-%Y %r')
You can make the result upper case later in your application code