I have a column in my SQL database that has values like 1:00 (meaning 1 AM), 13:56 (1:56 PM), etc. I would like to have the mysql command show only the variables after 7 AM. Is that possible? Thank you!
Presuming your column is named theTime, in a table named foo:
select * from foo where HOUR(theTime) >= 7;
If your column is not a date/time or timestamp field, you are in some trouble.
Related
MySQL 5.6 - we had a talk with one of the developers and found something very curious - couldn't find anything about it in google:
create temporary table md (dates date);
insert into md select '2000-01-01'; -- result: 2000-01-01
insert into md select '2000-00-01'; -- result: 1999-12-01 ????
insert into md select '2000-00-00'; -- result: 1999-11-30 ????
insert into md select '2000-01-00'; -- result: 1999-12-31 ????
insert into md select '2000-13-01'; -- result: EXPECTED: SQL Error [1292] [22001]: Data truncation: Incorrect date value: '2000-13-01' for column 'dates' at row 1
insert into md select '2000-12-49'; -- result: EXPECTED: SQL Error [1292] [22001]: Data truncation: Incorrect date value: '2000-12-49' for column 'dates' at row 1
Could someone please explain?
Why does it let this happen? It should throw the same Data issue error imho.
Documentation?
What is the exact logic behind this?
a) Month 00 previous month same day?
b) Day 00 previous month last day?
c) both 00-00 previous month's previous' last day?
Thanks!
https://dev.mysql.com/doc/refman/8.0/en/using-date.html
MySQL permits you to store dates where the day or month and day are zero. This is convenient if you want to store a birthdate in a DATE column and you know only part of the date. To disallow zero month or day parts in dates, enable the NO_ZERO_IN_DATE mode.
I have a fairly simple SQL table (using MYSQL Workbench 8.0) where the rows of the table contain dates and values like so:
date | value
--------------------|----------
2018-09-06 18:00:00 | 73
Values in the date column range from %18:00:00 to %17:30:00.
All I would like to do is return results from a query where I exclude all rows where the time in the date column is before 17:00:00.
The query I am currently using to no avail is:
SELECT * FROM table_name where td_time <> '%17%'
For reference, the values in column 'date' are formatted as type datetime.
I believe I'm missing something fairly simple, but this is my 2nd day using SQL and I cannot figure out if I am missing a small nuance to the syntax.
There are many function in MySQL to process time and date value, you can do like this:
select * from table where hour(date) < 17 and hour(date) > 17
Or
select * from table where time(date) < '17:00:00' and time(date) > '18:00:00'
Accidentally, my host had set the time for 2012 and in my database, there are more than 4000 records updated timestamp column as 2012-11-21 11:24:40.
I cant change time on all columns while updating, I need to keep the same time as per the table column, I just need to change only year 2012 to 2016 in that table.
I listed all columns using this query,
SELECT * FROM `table` WHERE `del_date` LIKE '%2012-11-21%';
but do not know to change the year only in that column.
Please help how can I change the same.
You can use ADDDATE(), like
UPDATE yourTableName SET del_date = ADDDATE(del_date, INTERVAL 4 YEAR);
I have a MySQL DateTime field which represent opening times.
I want to write a statement that will allow me to select rows from my table independent of the year supplied (Ex. *-12-17 00:00:00)
Try this:
SELECT * FROM table1
WHERE DAYOFMONTH(datecolumn) = 17
AND MONTH(datecolumn) = 12
Reference for DAYOFMONTH.
You did not say what you wanted to choose by if not the year: Go to the MySQL page and choose the correct functions.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
I have a scenario where I want to be able to SELECT rows from a MySQL table, but exclude rows where the current time-of-day is inside a time-range.
Example:
The "quiet" period for one row is 10pm - 8:30am.
My SQL SELECT statement should not return that row if the current server time is after 10pm or before 8:30am.
Example 2: The "quiet period" is NULL and ignored.
Example 3: A new row is created with a quiet period from 9:53am to 9:55am. If the current server time is in that 2-minute window, the row is not returned by the SELECT.
My question:
What data format would you use in the database, and how would you write the query?
I have thought about a few different approaches (defining start_time as one column and duration as another, defining both in seconds... or using Date stamps... or whatever). None of them seem ideal and require a lot of calculation.
Thanks!
I would store the start and end dates as MySQL native TIME fields.
You would need to consider ranges that span midnight as two separate ranges but you would be able to query the table like this, To find all current quiet periods
SELECT DISTINCT name FROM `quiet_periods`
WHERE start_time<=CURTIME() AND CURTIME()<=end_time
Or to find all non-active quiet periods
SELECT name FROM quiet_periods WHERE name NOT IN (
SELECT name FROM `quiet_periods`
WHERE start_time<=CURTIME() AND CURTIME()<=end_time
)
So with sample data
id -- name -- start_time -- end_time
1 -- late_night -- 00:00:00 -- 08:30:00
2 -- late_night -- 22:00:00 -- 23:59:59
3 -- null_period -- NULL -- NULL
4 -- nearly_10am -- 09:53:00 -- 09:55:00
At 11pm this would return
null_period
nearly_10am
from the second query.
Depending on performance and how many rows you had you might want to refactor the second query into a JOIN and probably add the relevant INDEXes too.