Delete timezone and time in MySQL - mysql

I have a problem. I have a table like this (example data):
value
timestamp
22.12
2023-01-18T08:00:35.000Z
22.18
2023-01-18T09:13:12.000Z
22.15
2023-01-18T09:16:12.000Z
22.17
2023-01-18T09:49:35.000Z
16.12
2023-01-25T10:15:05.000Z
26.18
2023-01-25T10:40:05.000Z
25.52
2023-01-25T10:55:05.000Z
19.88
2023-01-26T11:40:05.000Z
16.12
2023-01-16T12:40:05.000Z
I'am getting an average of values and I'am grouping it by date. I use:
select cast(timestamp as date) as dt, AVG(value) as avg_val
from tbl_name
group by cast(timestamp as date);
And my result looks like this:
22.01384610396165 2023-01-18T23:00:00.000Z
Is it possible to get only data without time and timezone?

Yes, it is possible to get only the date (without time or timezone) from the timestamp. You can use the DATE() function in your query to convert the timestamp to a date. The syntax would be:
SELECT DATE(timestamp) AS dt
, AVG(value) AS avg_val
FROM tbl_name
GROUP BY DATE(timestamp);
This will return the result as:
22.01384610396165 2023-01-18

Related

Hourly counting MySQL

Since few days, I am trying to count records per hour from the MySQL database.
I have a table with a lot of records and I have column DATE and column TIME where in DATE I have the date of the record in the format 2022-05-19, and in the column TIME, I have the time of the record in the format 14:59:38.
What I am trying is to count every single day how many records per hour I have. Something like this:
DATE HOUR PCS
22-05-18 06-07 11
22-05-18 08-09 20
......... ..... ..
....... 21-22 33
I have tried many different ways but no success.
For example:
SELECT 'Date', count(*) FROM `root4`
where
DATE between '2022-05-01' and '2022-05-1' AND
TIME BETWEEN '06:11:05' AND '07:11:05'
Any help is highly evaluated.
I would recommend not using reserved words for columns, as you will have to escape them a lot. https://dev.mysql.com/doc/refman/8.0/en/keywords.html
If you stored TIME as a timestamp, you can extract the hour using the HOUR() function and group by that:
SELECT
`DATE`,
HOUR(`TIME`) AS `HOUR`,
COUNT(1)
FROM your_table
GROUP BY
`DATE`,
HOUR(`TIME`)
If you happened to store it as text you can use REGEXP_SUBSTR to get the hour value from your time string.
SELECT
`DATE`,
CAST(REGEXP_SUBSTR(`TIME`, '[0-9]+') AS UNSIGNED) AS `HOUR`,
COUNT(1)
FROM your_table
GROUP BY
`DATE`,
CAST(REGEXP_SUBSTR(`TIME`, '[0-9]+') AS UNSIGNED)
You can format your HOUR column how you want, like displaying 01-02 instead of 1 by using CONCAT, but this is your basic setup.

MYSQL query with year and month

I can't find why DATE(created) LIKE '2016-1%' does not return from database anything, while DATE(created) LIKE '2016%' will return the row with date 2016-1-22, this column is timestamp format with CURRENT_TIMESTAMP.
SELECT * FROM logs WHERE DATE(created) LIKE '2016-1%'
I can't find the row either with 2016-1-22 ...
The DATE part of a TIMESTAMP column is retrieved by MySQL in the format 'YYYY-MM-DD' (see documentation). So a date like 2016-1-22 is actually represented as 2016-01-22, which does not match '2016-1%'.
Try prefixing month filed with 0, till 9(i.e. 01,02,03,..).
So your query should become like
SELECT * FROM logs WHERE DATE(created) LIKE '2016-01%'

How to select a date and ignor the time

I made a SQL Statement and I want to use the date but without the time.
My Select is:
SELECT DATEPART(dw, [Order].PerformDate)
And my Group by is:
GROUP BY [Order].PerformDate
Now how can I ignore the time?
You can use CONVERT function of SQL
select datepart(dw, CONVERT(DATE,[Order].PerformDate))
GROUP BY CONVERT(DATE,[Order].PerformDate)
Cast datetime value to date:
select cast(`Order`.PerformDate as date) as PerformDate
GROUP BY says "I want one result row per ____". In your case one row per PerformDate. If PerformDate is a datetime, but you only want to have one result row per date without time, then you must extract the date part:
group by cast(performdate as date)
You also want to display the weekday with datepart(dw, performdate) but this is no longer possible, because PerformDate is no longer available. Only its date part is. So:
select datepart(dw, cast(performdate as date))
from ...
group by cast(performdate as date);
Another one method:
select date_format(`order`.PerformDate, '%Y%m%d')
...
group by 1

How to filter a timestamp format using Mysql?

I just want to ask how can i filter a timestamp value in Mysql?
Let's say we have the following datetime as
1351128031
1351128045
1351128097
How can I create a date range using this format?
How can I perform this in a query?
Like this:
SELECT * FROM user
WHERE acct_created BETWEEN (datefrom) AND (dateto) -- my problem is I can't filter the timestamp
You want to use a mysql function called UNIX_TIMESTAMP http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
SELECT * FROM user
WHERE acct_created BETWEEN UNIX_TIMESTAMP('datefrom') AND UNIX_TIMESTAMP('dateto')
where datefrom and dateto are dates formated as string similar to '2012-01-01 00:00:00' in UTC
Here is the SQLFiddel Demo
Below is the MySQL Select Query :
select *,
UNIX_TIMESTAMP(`Timestamps`)
from Table1
where UNIX_TIMESTAMP(`Timestamps`) between 1351128040 and 1351128099

Find results in certain date range

I am trying to only grab records that fall in a certain date range. The problem is that the timestamp and the date are stored as a string in the same cell. I want to only grab rows with a date that falls betweed 2013-05-01 and 2013-05-03.
date (stored as string)
2013-05-01T23:19:44
2013-05-02T23:19:40
2013-05-06T23:19:46
2013-05-06T23:15:17
mysql
SELECT * FROM table WHERE date BETWEEN 2013-05-01 AND 2013-05-03
Try
SELECT *
FROM table1
WHERE STR_TO_DATE(`date`,'%Y-%m-%d') BETWEEN '2013-05-01' AND '2013-05-03'
SQLFiddle
As #FreshPrinceOfSO absolutely correctly noted no index will be used in that case
SELECT * FROM table1
WHERE STR_TO_DATE(SUBSTRING(`date`,1,10),'%d-%m-%Y')
BETWEEN '2013-05-01' AND '2013-05-03'
The string is almost valid syntax for a datetime. Thus, an alternative, if perhaps slower method is to replace the 'T' with a space and then cast it to a datetime.
SELECT * FROM table1 WHERE
CAST(REPLACE(`date`, 'T', ' ') AS DATETIME)
BETWEEN '2013-05-01' AND '2013-05-03';
SELECT * FROM table
WHERE date('yourdate') BETWEEN date('2013-05-01') AND date('2013-05-03')