Query data between the same date - mysql

Is it possible to do something like this?
select * from table where Date BETWEEN '2019-05-29' AND '2019-05-29'

Yes it is possible. If you have time part you could use DATE function to skip it:
SELECT * FROM table WHERE DATE(Date) BETWEEN '2019-05-29' AND '2019-05-29'
-- it may degrade performance, condition is not SARGable

Yes, but the better approach is:
select t.*
from table t
where t.Date >= date('2019-05-29') AND
t.Date < date('2019-05-29') + interval 1 day
Why is this better? It doesn't have a function on the column name, so it can make use of an index on the date column.

Yes you can, if you want to run it in a test window without manually changing the date within the code you can set it as a variable. Use trunc to get rid of time i.e there will be no 29-05-2019 23:59:00. If you want the same date within a time period remove the trunc and then you can set hours-minutes-seconds which makes your query more precise
SELECT t.*
FROM table t
WHERE t.date BETWEEN trunc(to_date(:datefrom, 'dd.mm.yyyy hh24:mi:ss')) AND
trunc(to_date(:dateto, 'dd.mm.yyyy hh24:mi:ss'))

Related

How can I find the greater then and less then date with the available date

As you show in the below images I want to fetch result in between date from date range.
I have try this query but it doesn't work.
I want bigger date then DTT_EVT_start start table date.
Can anyone suggest me how can I use date(not time)column in where clause ?
SELECT *
FROM `dwssgv_esp_datetime`
WHERE (DATE_FORMAT(DTT_EVT_start, '%Y-%m-%d') < '2015-05-10')
AND (DATE_FORMAT(DTT_EVT_end, '%Y-%m-%d') > '2015-05-10')
Something like this?
SELECT * FROM dwssgv_esp_datetime WHERE DTT_EVT_start < '2015-05-10' AND DTT_EVT_end > ''2015-05-10'
You can avoid DATE_FORMAT function,
as it would affect on the performance if its a huge table
you may simply do like,
SELECT
*
FROM
dwssgv_esp_datetime
WHERE
DTT_EVT_start < CAST('2015-05-10 00:00:00' AS DATETIME) AND
DTT_EVT_end > CAST('2015-05-10 00:00:00' AS DATETIME)
Here, < 2015-05-10 > is your variable date.
OR
SELECT
*
FROM
dwssgv_esp_datetime
WHERE
DATE(DTT_EVT_start) < CAST('2015-05-10' AS DATE) AND
DATE(DTT_EVT_end) > CAST('2015-05-10' AS DATE)
You can convert dates in UNIX_TIMESTAMP format and compare
Try this
SELECT
*
FROM
`dwssgv_esp_datetime`
WHERE (UNIX_TIMESTAMP(DTT_EVT_start) < UNIX_TIMESTAMP('2015-05-10')
AND (UNIX_TIMESTAMP(DTT_EVT_end) > UNIX_TIMESTAMP('2015-05-10')
Here is a simple solution:
SELECT *
FROM dwssgv_esp_datetime
WHERE
CAST('2015-05-10' AS DATE)
BETWEEN DATE(DTT_EVT_start)
AND DATE(DTT_EVT_end)
BETWEEN AND allows you to check if a value is in a particular range or not. By using this you do not have to write the value you want to compare twice.

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.

Sql - find if date is between two dates

I have a field of FromDate and a field of ToDate.
I am looking for the rows that today is between the "from" and "to"
select * from job
where job.type='manager'
and '2014-01-22' between job.FromDate and job.ToDate
The query does not throw an exception , and it even returns some rows. But it isn't right- the rows it returns do not have the dates I am looking for.
P.S. the date format I am using is the correct one for my DB.
Try this
select * from job
where job.type='manager'
and job.FromDate <= '2014-01-22' and job.ToDate >= '2014-01-22'
Comparing dates is often tricky, especially if the values are stored as datetime and not date. The time components can affect the comparison. Another possibility is that ToDate is NULL for the most recent records. Here is one way to fix this:
select *
from job
where job.type ='manager' and
date('2014-01-22') between date(job.FromDate) and date(coalesce(job.ToDate, '2099-12-31'))
However, the use of the function on the columns can make the query less efficient. Instead, you might try:
select *
from job
where job.type ='manager' and
job.FromDate < '2014-01-23' and
(job.ToDate >= '2014-01-22' or job.ToDate is null);

using the difference between two dates in a WHERE clause

I'm trying to filter a SELECT query between NOW() and NOW - interval 10 minute(?), but i can't seem to get this to work, and it's given me a few questions on the topic.
I've looked through some documentation online, and alot of questions on stackoverflow but non of the solutions give me what i need. Looking at the TIMEDIFF and TIMESTAMPDIFF documentation, i only see it used like this;
SELECT TIMESTAMPDIFF(SECOND,'2007-12-30 12:01:01','2007-12-31 10:02:00');
However i don't want to just select the time difference, i want to use it in a query as a WHERE clause, something like;
SELECT * FROM tableName WHERE (the time difference betweeen NOW() and the stored timestamp is less than x minutes);
Is there a particular data type i need to set my column to?
How can i use the TIMEDIFF / TIMESTAMPDIFF correctly, and if these are not the correct methods i should be using, what is?
SELECT * FROM tableName WHERE TIMESTAMPDIFF(MINUTE,timestamp,NOW()) < 10
SELECT * FROM tableName
WHERE now() - interval 10 minute < stored_timestamp

How can I return results less than current date?

I want to return results from my table that have a date less than today's date. Here is my statement:
$today = date('Y-m-d');
SELECT * FROM events WHERE STR_TO_DATE('$today', '%Y-%m-%d') > wp_eventscalendar_main.eventStartDate
I can select events in the future without a problem but when I try to select events in the past I don't get any results. My column 'eventStartDate' is set as a Date. Is there some kind of special operator I should be using for this?
Thanks,
You probably need:
SELECT * FROM events WHERE CURDATE() > wp_eventscalendar_main.eventStartDate
MySQL tries to compare a String to a Date, which won't work reliably.
You can Use
select from table_name where date < curdate()