SQL query to select data between dates does not show last date - mysql

im using a query to get data between dates but for some reason it does not pull the data of the last date selected here is my query:
SELECT * FROM order WHERE status = "completed" AND orderdate >= ? AND orderdate <= ? ORDER BY orderid DESC
Im using is equal to or less then... but still?
what am i doing wrong ?

SELECT * FROM order WHERE status = "completed" AND date(orderdate) >= date(?) AND date(orderdate) <= date(?) ORDER BY orderid DESC

It happened with me also, but in my case instead of passing a date I was querying using a datetime variable, Please make sure you are querying with date variable only.
Make sure that orderdate is date as well as your query parameter is also date, or use appropriate function to convert them in date, than query.

Your dates are actually datetimes - so you are actually, in the case of the upperbound, saying "12 midnight" on whichever date you choose. Hence, if it tries to test a value at say 10am in the morning, it fails as being outside the range.
Either set the upperbound date one day forward, or explicitly only test the date part of the datetime...

Related

Select only date form datetime field in where condition

I have stored this kind of format 2022-02-06 18:40:00 in my trans_reminder_date. I want to use only date in where condition but with this condition i am not able to fetch data
$today = date('Y-m-d');
SELECT * FROM sales_detail
WHERE trans_reminder_date = '".$today."'
AND trans_reminder_date != ''
ORDER BY sales_detail_id DESC";
If when your filter parameter is a string:
select * from sales_detail
where cast(trans_reminder_date as date) = cast('2020-03-22' as date)
if you want to use the current date for filtering then MySQL has a function that getting only the current date without time.
select * from sales_detail
where cast(trans_reminder_date as date) = curdate()
On MySQL for converting other types to another, you can use a cast
P.S.
Starting with MySQL 8.0.13 we have now an easiest way to create functional indexes. When you are using cast(updated_at as date) then DB will not use index for column updated_at. You must create a functional index for best performance.
The following query will give you all the result for the current date. By doing this there won't be any need to cast values.
SELECT
*
FROM
sales_detail
WHERE
trans_reminder_date >= curdate()
AND trans_reminder_date < curdate() + INTERVAL '1' DAY
ORDER BY
sales_detail_id DESC;
Using Cast function to change datetime type to date example
Cast(column_name as date)
Or
You using convert function change datetime type to date
CONVERT(column_name, date);
CONVERT(expression, datatype);
OR,
CONVERT(expression USING character_set);
character_set: It specifies the desired character set in which we want to be converted.
Data type : It specifies the desired data type in which we want to be converted.
Expression : It is a specified value going to be converted into another specific datatype.

Rows with a start_date and end_date, need to return records that fall within a date range

I have a table that contains tasks, each task has a date_start and date_finish field.
I need to construct a query which will take a passed in date and return all rows if that passed in date falls between the date_start and date_finish.
Does this make sense?
I have been trying to use standard date type querys such as:
SELECT *
FROM project_task
WHERE project_task.date_start >= '2013-10-10' AND project_task.date_finish <= '2013-10-10'
but it doesn’t return the correct results and using BETWEEN does not work either because I need it to take into account both fields (date_start and date_finish) not just the one.
I think it may only be the WHERE part of the query I need.
You've made a simple mistake, you got the order of your operators wrong:
SELECT *
FROM project_task
WHERE
project_task.date_start <= '2013-10-10' -- start should before the test date
AND
project_task.date_finish >= '2013-10-10' -- and finish after the test date
Explanation
If the date checked is between date_start and date_finish, then we must have reached at last the start date. We could have a later date too. That means the
date_start will be lower or equal than the date checked
And the second check said: the finish date musn't have passed. So
date_finish has to be greater or equal than the date checked.
With your original query you will only get projects that start and end on '2013-10-10'.
Demo
DECLARE #Now datetime = '2013-10-10T00:00:00'
SELECT *
FROM project_task a
WHERE #Now >= a.Date_Start
AND #Now < isnull(a.Date_Finish, '9999-12-31T00:00:00')

Mysql Select with Dates and maybe Case when

im having a problem where i cant think of a solution, maybe im having a bad table-structure or i just dont know enough about mysql select commands to think of a good solution. Maybe you can help me out:
So i got a table that has a Column with the Date-format (yyyy-mm-dd) i wanted to select all upcoming dates so i did:
SELECT * WHERE date >= now.
This worked kinda well but i also got "dates" where only the year is entered (2014-00-00) i also wanted to select these but "now" is already bigger so i made another column with the year only and if the month, date or both arent known i will use 0000-00-00 and the Column "year" now i could select like this:
SELECT * WHERE date >= now AND year >=now(year)
Now all entrys with 0000-00-00 wont be selected. If i use OR the entrys from last year will be shown.
So thats my problem, is there any way i can change my table so i can have entries with only the year or only year and month and of course all together? I already considered get rid of the date-format and use simple INT with seperated columns for year, month and date. But i think i will have the same problem.
Sometimes i just want to do a capsuled select like
SELECT *
WHERE (date >= now AND year >= now(year))
OR date == "0000-00-00" (i know that this doesnt work)
If I understood your problem correctly, you could use this request:
WHERE (date >= now OR year > now(year))
There is probably a simpler way though, that would preserve your design, like initializing at January 1st (01-01) instead of 00-00
I think you can use this code:
$_SESSION['month'] = //set here your selected month
$_SESSION['year'] = //set here your selected year
SELECT * FROM table WHERE DATEPART(m,date) >= '".$_SESSION['month']."' AND DATEPART(yyyy,year) >= '".$_SESSION['year']."' AND date <> '0000-00-00'
Change your table structure format. Actually just allow for that field to have null value when not entered. By default it will be null then. You shouldn't be storing 0000-00-00 as a value for Date type field. I would rather leave it as null , or as suggested in some of previous answers, initialize it with some other date. It would be much easier to manipulate with database then.
the problem is that half of you write is not MySQL and your database schema is terrible...
You have the following problems:
column data date does not have the date data type.
To fix it, you need to add a cast to the select statement eg. cast(datecolumn as date)
select * from table where cast(datecolumn as date) >= '2014-01-10';
the way to use now date is using the now function.
select now(), date(now());
result> 2014-01-10 11:11:36, 2014-01-10
select * from table where cast(datecolumn as date) >= date(now());
Because your datecolumn is not a date (2014-00-00 is not a valid date), you need to use string manipulation to extract the year.
select substring('2014-01-01', 1,4)
result> 2014
select * from table where substring(datecolumn, 1,4) = year(now());
The comparassion operator is = and not ==
the select statement syntax looks like this (pay attention because you are missing the table in your statement)
select * from [Table] where [column] = condition ...
You probably need or instead of ands, therefore your query should look like this:
select * from FooTable where
cast(datecolumn as date) >= date(now())
or substring(datecolumn, 1,4) >= year(now())
or datecolumn = '0000-00-00'
You should use something like phpmyAdmin or mySQL workbench to test your sql queries before try to use them on php, java or whatever is your programing language.

MYSQL UNIX_TIMESTAMP not converting

I am trying to pull data from my DB and have it organized by DATE by using the UNIX_TIMESTAMP but it just seems to not work properly. This is just one of my failed attempts. the issue that I am encountering is that I am trying to convert a Field in my table to a UnixTimeStamp, but it doesn't seem to work. the purpose of this query is to pull all future birthdays, and if the birthday has passed, we give it a '0' value in the hasItPassed column, if not, we give it a '1' value. the way I am trying to organize this table is have all upcoming birthdays listed (have the closest birthday at the top) and have all birthdays that have passed at the bottom of the list.
(EXAMPLE) bDay.date format --> '10/07/2013'
So my question is, does anyone know the best way to convert a date field in your DB to a unixTime format?? what happens in this example is in my field hasItPassed, everything gets turned into 0 when in fact some of the dates haven't passed(so some should have a 1 instead of a 0). Therefore I know I'm having issues with the conversion.
SELECT bDay.id, bDay.userid, bDay.summary,
bDay.time_create, bDay.Address, bDay.date, bDay.time,
CASE WHEN UNIX_TIMESTAMP(bDay.date) >= NOW( )
THEN '1'
ELSE '0'
END AS hasItPassed
FROM
BirthdayTable
WHERE
ORDER BY hasItPassed DESC ,
CASE WHEN hasItPassed =1
THEN date
END ASC ,
CASE WHEN hasItPassed =0
THEN date
END DESC
If there is any confusion, please let me know and I will reformat the question. thank yall!!!
Either make your comparison compare dates or timestamps - you have it mixed up.
e.g.
bDay.date >= date(NOW())
or
UNIX_TIMESTAMP(bDay.date) >= UNIX_TIMESTAMP(NOW())

How to check if selected date range is between another date range

I am in the situation where i want to match a date range with another date range, it might be simple but i am stuck at it.
Below is table structure
Table - lifecycles
life_id
life_start_date
life_end_date
then a few records as below
1 - 07/23/2013 - 07/24/2013
2 - 07/15/2013 - 07/25/2015
3 - 03/10/2013 - 03/10/2014
Now i want to search these records by date range and want to see if some life exists in that range; e.g. i want to find the lives between 08/01/2013 - 01/01/2014
As expected result it should select the life#2 and life#3
How can this be done with MySQL query?
Any help is highly appreciated.
This query should do it:
SELECT
*
FROM
lifecycles
WHERE
str_to_date(life_start_date, '%m/%d/%Y') <= '2014-01-01'
AND str_to_date(life_end_date, '%m/%d/%Y') >= '2013-08-01';
Which basically means life hasn't started before the end of the range you are looking for, and life didn't end before the range start.
Since you keep dates in VARCHAR format, you need to use str_to_date function, which is bad since MySQL won't be able to utilize any possible indexes you have on start_date or end_date columns.
This might help you.
SELECT SUM( IF( '2014-01-02' BETWEEN from_date AND to_date, 1, 0 ) ) AS from_exist,
SUM( IF( '2014-02-12' BETWEEN from_date AND to_date, 1, 0 ) ) AS to_exist
FROM date_range
So based on the results you can check whether date is between existing date range or not.
So you want to exclude lifes that are ended BEFORE 08/01/2013 and the ones that are not started AFTER 01/01/2014. This should work:
SELECT *
FROM lifecycles as alive
WHERE NOT EXISTS (
SELECT 1
FROM lifecycles as dead
WHERE dead.life_id = alive.life_id
AND (str_to_date(life_start_date, '%m/%d/%Y') > '2014-01-01'
OR str_to_date(life_end_date, '%m/%d/%Y') < '2013-08-01'))