MYSQL query between two unix timestamp - mysql

How to query data in specific time like: I want to select all records stored between 1 pm and 5 pm during a month , when the time stored in datetime column in Unix timestamp format like so "1403830861".

The FROM_UNIXTIME function can convert a unix timestamp to a date. The %k format (hour represented as an in 0..23) seems to fit the bill nicely. The month could be easily extracted in the same fashion, using the %m format and the year using %Y. E.g., the following query would return only results from November 2014:
SELECT *
FROM my_table
WHERE FROM_UNIXTIME (timestamp_column, '%k') BETWEEN 13 AND 17 AND
FROM_UNIXTIME (timestamp_column, '%m') = 11 AND
FROM_UNIXTIME (timestamp_column, '%Y') = 2014

Related

convert to 24 hour format in mysql

I have a query selecting time in the format where the time column is of the DateTime datatype.
SELECT ul.time FROM user_logins;
It is returning the result in this format-
11/27/2021 9:29:46 AM
11/23/2021 12:48:20 PM
Now I want it to return in the 24 hour format like
11/27/2021 9:29:46 11/23/2021 00:48:20
Is there any possible way to achieve that?
DATETIME values always stored in 24 hour format. As your column defined as DATETIME datatype, it's in 24 hour format
Output of DATETIME datatype:
2021-12-07 18:21:30.907
AM / PM is not mentioned by DATETIME, So We can format it.
Query: Select CONVERT(varchar,GETDATE(),9)
Output: Dec 7 2021 7:26:55:077PM

how to query data between AM and PM varchar time in MySQL [duplicate]

This question already has answers here:
Converting an AM/PM time to 24 hours format using PHP or MySQL?
(4 answers)
Closed 1 year ago.
I have date and time stored as 2 columns in MySQL table using type varchar. Time format is 12 hour format (11:59:59 am, 12:59:59 pm, 01:59:59 pm, etc.) as in attached files.
I'd like to query to get data between 11:13:00 am and 12:13:59 pm. The query I used as
SELECT * FROM `table` WHERE date = '10-09-2021' and ((time BETWEEN '11:13:00 am' and '11:59:59 am') or (time BETWEEN '12:00:00 pm' and '12:13:59 pm'))
It give me all data including pm data like 11:20:23 pm in attached files. Please advise how to query. Alternative way is I'd like to get data within 1 hour range. Thank you.
To query on pure varchar type, it needs to filter am/pm
SELECT *
FROM `table`
WHERE `date` = '10-09-2021'
AND ((`time` LIKE '% am' AND `time` BETWEEN '11:13:00 am' and '11:59:59 am') OR (`time` LIKE '% pm' AND `time` BETWEEN '12:00:00 pm' and '12:13:59 pm'))
to query one hour range,
`time` LIKE '11:% am'
Other option is to convert the varchar time to a date using STR_TO_DATE(), and use standard between ... and ... , side effect is that you probably cannot use the table index on the column.

mysql select on datetime objects within range

I have a table 'appointments' that contains, among other things, two datetime fields labeled 'start' and 'end'. I also have date in local time that is converted as a range of a full day into UTC (which is what the SQL table stores the datetimes as). I need to select all the (business) times between 00:00:00 and 08:00:00 UTC that also fall in the range of my local time conversion.
An example, A user in PST (pacific standard time) picks December 1st, 2018. The dates between December 1st at 00:00:00 and December 2nd 00:00:00 are converted to UTC which would be December 1st 08:00:00 to December 2nd 08:00:00. I need to select all appointments between 00:00:00 and 8:00:00 any given day in the previous range (dec 1 - dec 2).
All of my datetimes/queries are in the form 'yyyy-MM-dd HH:mm:ss'.
I know that I can select all of the times between two times rather simply like so:
SELECT start, end
FROM appointment
WHERE start>='2018-12-01 00:00:00'
AND end<='2018-12-02 08:00:00'
But I'm unsure as to how to trim these down to only between business hours.
I'm looking for something like
SELECT start, end
FROM appointment
WHERE (start>='2018-12-01 00:00:00'
AND end<='2018-12-02 08:00:00')
AND (start.substring(11, start.end) >= '00:00:00'
AND end.substring(11, end.end) <= '08:00:00')
Where a call like start.substring(11, start.end) would return the time in 'HH:mm:ss' format
Try using the TIME function in MySQL.
SELECT start, end
FROM appointment
WHERE TIME(start) >= '00:00:00'
AND TIME(end) <= '08:00:00' AND ... //other conditions

Get 5 days back records with specific date formate with mysql

I want to select records of 5 days ago with mysql, but the problem is that my date column is varchar and in the following format yyyy-mm-dd 00:00:00 AM/PM.
How can I convert this date format to mysql date and select records from 5 days back?
my date column name is 'date_time'
This is a job for STR_TO_DATE().
Try this.
... WHERE DATE(STR_TO_DATE(date_time,'%Y-%m-%d %h:%i:%s %p')) = CURDATE() - INTERVAL 5 DAY
The '%Y-%m-%d %h:%i:%s %p' DATE_FORMAT() string matches your textual date format.
This will never be fast if you have to search through a large number of rows, because it isn't sargable. It can't exploit an index.

MYSQL query: select between date with local format

my mysql database tb_date (varchar 20):
16 November 2014
06 December 2014
01 April 2014
12 April 2015
I want select between 01 January 2014 until 31 December 2014, how the query is with date conversion?
thanks..
This is an anti-pattern, storing date values in VARCHAR columns, rather than using datatypes specifically designed and implemented for storing date values... DATE, DATETIME or TIMESTAMP.
To answer your question, before it gets closed, you could use the STR_TO_DATE function to convert the strings into DATE datatype, and then do the comparison. MySQL won't be able to make use of an index range scan operation, it will need to evaluate that function on every flipping row in the table.
As an example:
SELECT t.mycol
FROM mytable t
WHERE STR_TO_DATE(t.mycol,'%d %M %Y') >= '2014-01-01'
AND STR_TO_DATE(t.mycol,'%d %M %Y') < '2015-01-01'
We'll need to check the MySQL Reference Manual to verify that '%M' is the right format specifier for the full month name...
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Yes, it looks like I guessed right. M is the month name.
As I already commented, you should store your date as Timestamp or Data format then you could simply compare.
However, there is still a solution.. You can convert the varchar to a date directly in your query :
select * from yourTable
where (str_to_date(tb_date, '%d %M %Y') between '2014-01-01' and '2014-12-31');
But please don't use this hack and change your date format...
Edit : If you are really willing to use varchar to store your date, change it to varchar(17) which is the max character possible using your string format.