Given "input date" is between two dates or not? [closed] - mysql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Table structure
id from(date)_field to(date)_field field3 field4
1 date1 date2 something something
2 date3 date4 something something
3 date5 date6 something something
. . . . .
. . . . .
I want to find out the top 1 row where the from(date)_field is between the user_input_date and from(date)_field.

You can do:
select t.*
from t
order by abs(datediff(fromdate, #yourdate))
limit 1;
More typically, you would not want the closest date. You would want the row where the date is between the to and from dates. However, you are explicitly asking for the closest.

Use datediff and find the smallest value.
SELECT DATEDIFF(second, fromdate, Userdate) AS DateDiff;
SELECT DATEDIFF(second, Todate, Userdate) AS DateDiff1;
And compare DateDiff and DateDiff1 which have lowest value that would be closest one

Related

Query for get lastupdate datetime in redudant date table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to get lastupdate datetime order by updateTime but prevent for redundant date in my output
desired result
2021-06-25 15:46:57
2021-06-26 15:48:52
2021-06-27 17:11:52
2021-06-28 17:17:33
2021-06-29 15:16:29
I tried this
SELECT t.updateHistoryID, t.updateTime
FROM web_historyupdate t
INNER JOIN ( SELECT updateHistoryID, max(updateTime) as maxdate
FROM web_historyupdate
GROUP BY updateHistoryID ) tm
ON t.updateHistoryID =tm.updateHistoryID
AND t=tm.maxdate
You want the maximum datetime per date. As mentioned, "Maximum" translates to MAX in SQL and "per" transates to GROUP BY. One way to apply this to your data:
select *
from web_historyupdate
where updateTime in
(
select max(updateTime)
from web_historyupdate
group by date(updateTime)
)
order by updateTime;
There exist of course other ways to do this. You could for instance select every row for which NOT EXISTS a row on the same day at a later time.

Between And Greater Than Date Should Be Implement In Same Query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two DateTime columns like start_datetime and end_datetime. these fields contain DateTime value.
Here itself I need three different outputs. while you search, "15-09-2020 00:00:00" to "26-09-2020 23:59:59" it will display the first two lines. if you search like "26-09-2020 00:00:00" to "13-10-2020 23:59:59", it should result in three rows.
Instead of if you search, "10-09-2020 00:00:00" to "18-09-2020 23:59:59", it will show the first two rows.
In this case, I am stuck at the query. if I am using between some results won't come. if I am using greater than a symbol, some results won't come.
store the date time format in YYYY-MM-DD H:I:S, so you can easily
SELECT *
FROM table_name
WHERE start_datetime BETWEEN value1 AND value2 AND end_datetime BETWEEN value1 AND value2;

SQL query for date change for upcoming events [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to run a query in sequelize or SQL where select current month data and only show just upcoming date data like some kind of events if that day pass then next event or date will be showing like this I have 3 dates in this month
1 2020-03-15
2 2020-03-22
3 2020-03-27
So before 15 I want to only no 1 date after 15march pass I want only no 2 date and goes on
If you want to show the next date in the future, then it would be something like:
select t.*
from t
where date > current_date
order by date asc
fetch first 1 row only;
The exact syntax might vary by database -- say now() or getdate() instead of current_date; or select top (1) or limit 1 instead of the fetch clause.

MySQL - Month SELECT command [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am currently trying to solve a MySQL exercise and I am having issues with finding the right command.
There is a table called 'orders' with the following columns: orderNumber, orderDate, requiredDate, shippedDate, status and customerNumber.
The question says to show the orders payed in the summer (columns to be shown: orderNumber and orderDate).
I think the answer should be something like this, but I can't figure out how to precise the month (probably something with IN or BETWEEN but I can't solve it):
SELECT COUNT(orderDate), orderNumber
FROM orders
GROUP BY orderNumber;
You Use the MONTH function to get the month for the date and compare it with the summer period using BETWEEN
SELECT orderNumber, orderDate
FROM Orders
WHERE MONTH(w) BETWEEN x AND y
AND status = z
w is the date column to use, not sure which one it is
x & y represents start and end month of summer where you live
z the status the order gets when it’s paid
Is not clear how you distinguish between paid and unpaid but, anyway, for select the order, if you want the order list you could use
SELECT orderDate, orderNumber
FROM orders
where orderDate between str_to_date('2018-06-21', '%Y-%m-%d')
and str_to_date('2018-09-21', '%Y-%m-%d')
the aggregationion seems not necessary to me ..

SQL: Get date from table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a column which stores datetime like 2011-01-01 01:01:01 .
I need to get list of date:
2011-01-01
2011-02-01
Is there any way I can list down date from datetime in a table?
If the column is really a datetime, just use the date() function:
select date(column)
from table t;
Actually, this also works if the column is a string, assuming it is in YYYY-MM-DD format.
If you want a unique list of dates:
select distinct date(column)
from table t;
select substr(field,1,10)
For just a list of the dates.
SELECT DATE_FORMAT(field, '%Y-%m-%d')
SQL
select to_char(date_column,'YYYY-MM-DD') from table;
MySql
SELECT STR_TO_DATE(date_column,'%Y-%m-%d') from table;
You can use DATE_FORMAT().
Syntax:
SELECT DATE_FORMAT('2011-10-10 19:46:00', '%M %d, %Y');
Try this code
SELECT CONVERT(date,Column_Name) As Date FROM Table_Name
thank you for all answers.
what i really need is year. sorry for the misleading question.
the correct query for that is:
select distinct YEAR(created_on) as years from contacts
thank you