Query to select date range from different columns - mysql

I have a from_date and to_date columns in my LeaveApplication table. So I'm trying to check if the specified date is in range of the two columns. For example:
from_date : 2021-08-26
to_date : 2021-08-29
date : 2021-08-27
I was trying to use the BETWEEN operator but the syntax is to select the date range from the same column name.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
In my case, I want to select the date range from different columns something like the query example below. Is there a way to achieve this?
SELECT * FROM LeaveApplication
WHERE "2021-08-27" BETWEEN (from_date = "2021-08-26") AND (to_date = "2021-08-29")
Thank you!

Using greater, equal, and less operator didn't work for me but actually it can be done with BETWEEN operator this way
SELECT * FROM LeaveApplication WHERE "2021-08-27" BETWEEN from_date AND to_date
Found this example here
p/s: my attempt in my example is quite close to it :)

select * from LeaveApplication where from_date >="2021-08-26" and to_date <="2021-08-26"
If the result of rows is empty, the date exists, else not!

Related

Select the latest year in a column using SQL query

I have a dataframe like this
Value.
Date
A
08/08/2009
A
09/12/2021
A
05/10/2022
A
06/09/2022
A
07/08/2022
I need output like
VALUE
DATE
A
05/10/2022
A
06/09/2022
A
07/08/2022
We have to print a latest year with all month data present in the date column .please refer output table.
i used SQL query like
Select Top 10 * from table where
Order by (Date) DESC;
The max() select only one date so that didn't help me
But didn't get expected answer.
Can please someone help me with the query ?
You can just use MAX in a subquery, this will produce the intended outcome you have shown in your question:
SELECT yourcolumn
FROM yourtable
WHERE
YEAR(yourcolumn) = (SELECT MAX(YEAR(yourcolumn)) FROM yourtable);
The latest year is 2022, so MAX in the subquery will find this year and the whole query will select all dates in 2022.
SELECT *
FROM tablename
WHERE datecolumn >= (SELECT DATE_FORMAT(MAX(datecolumn), '%Y-01-01')
FROM tablename)
To improve this query you'd have an index by datecolumn (or where this column is an expression prefix).

How to apply date filter also to columns which don't have it

I have a query like:
SELECT * FROM TABLE WHERE date <= '2017-05-16 20:00:00'
But this doesn't find rows which don't have that column date filled. I want to treat them as 0000-00-00 00:00:00 so I just want my query to find those rows too. Any suggestions?
You can use explicit logic:
SELECT *
FROM TABLE
WHERE date <= '2017-05-16 20:00:00' OR date IS NULL;
I would advise you to put in an explicit default date (even '0000-00-00'). It is not obvious that NULL means "the earliest possible date".

mysql - how to select data from next month

i have table with id, date_from, date_to fileds.
I need to find all the data where date_to +1 month is equal to condition
for example>
if data in date_to field is 2015-06-01 and WHERE condition is 2015-07-01 i should get the result
SELECT *, (date_to + INTERVAL 1 MONTH) as 'next' FROM table_name WHERE 'next'='2015-07-01'
this is my query, that obviously doesn't work ;)
Tnx in adavance
First thing you can not use alias in the where condition and also the condition is wrong. It will be
SELECT * FROM table_name
WHERE
date_add(date_to,interval 1 month)='2015-07-01'
Now note that for large data-set its not a good idea to use function on the date filed since even if the field is index it will fail to use index. So better to use the function on the data to be filtered rather than on the field. So the above query could be written as
SELECT * FROM table_name
WHERE
date_to= date_sub('2015-07-01',interval 1 month)

SQL Server : sorting of date null values at last row

I have a problem here in sorting of date that has some of entries are null I want the null values to be in the last row. I've already tried a solution found here in stack overflow the code is like this.
SELECT * FROM table
WHERE ...
ORDER BY CASE WHEN date IS NULL THEN 1 ELSE 0 END, date ;
but it doesn't work at all it always give me a null value any idea?
Try like this
SELECT * FROM table
WHERE ...
ORDER BY
CASE WHEN date IS NULL THEN '9999-12-31'
ELSE date
END, date ;
9999-12-31 is Maximum Date in SQL 2008
Hope This help.
This one answers your question Jade. Non empty dates are in ascending order and Null dates appear at last.
Select * from Your_Table Order by Your_Date_Column DESC

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')