Compare dates using dayofyear() function - mysql

I want to compare two dates trans-date and proc-date using dayofyear function. The function doesn’t work if the two dates are in separate years. Can anyone help me with that?
Example:
trans-date = 2019-01-12”
Proc-date= “2020-01-01”
Dayofyear(trans-date)=12
Dayofyear (proc-date) = 1
Logically, proc-date is greater than trans-date but dayofyear doesn’t work that way. So, how can I calculate the dates where I can get trans-date < proc-date?
Thank you.

Perhaps you want to compare the month/date without the year. If so, you date use date_format():
where date_format(trans_date, '%Y-%m') < date_format(proc_date, '%Y-%m')

Related

how to calculate turned around datetime? [duplicate]

How do I extract the month and date from a mySQL date and compare it to another date?
I found this MONTH() but it only gets the month. I looking for month and year.
in Mysql Doku:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_extract
SELECT EXTRACT( YEAR_MONTH FROM `date` )
FROM `Table` WHERE Condition = 'Condition';
While it was discussed in the comments, there isn't an answer containing it yet, so it can be easy to miss.
DATE_FORMAT works really well and is flexible to handle many different patterns.
DATE_FORMAT(date,'%Y%m')
To put it in a query:
SELECT DATE_FORMAT(test_date,'%Y%m') AS date FROM test_table;
If you are comparing between dates, extract the full date for comparison. If you are comparing the years and months only, use
SELECT YEAR(date) AS 'year', MONTH(date) AS 'month'
FROM Table Where Condition = 'Condition';
SELECT * FROM Table_name Where Month(date)='10' && YEAR(date)='2016';
You may want to check out the mySQL docs in regard to the date functions. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
There is a YEAR() function just as there is a MONTH() function. If you're doing a comparison though is there a reason to chop up the date? Are you truly interested in ignoring day based differences and if so is this how you want to do it?
There should also be a YEAR().
As for comparing, you could compare dates that are the first days of those years and months, or you could convert the year/month pair into a number suitable for comparison (i.e. bigger = later). (Exercise left to the reader. For hints, read about the ISO date format.)
Or you could use multiple comparisons (i.e. years first, then months).

Selecting from date range mysql/phpmyadmin problem

For my current project I'm using XAMPP. I have a problem on pulling data from a date range, sorry for the messy title I don't know how to word it out. The problem is I'm pulling dates from 06/01/2020 to this day 06/16/2020 however it doesn't display the data for today, but when my end date is 06/17/2020 (which is tomorrow at least in my country) it appears. Thank you in advance!
Did you use only greater or less >,< operators or also with equal values <=,>=?
You can also use BETWEEN function in your query.
WHERE date BETWEEN CAST('2020-06-01' AS DATE) AND CAST('2020-06-16' AS DATE);
Which is equivalent to:
WHERE date >= CAST('2020-06-01' AS DATE)
AND date <= CAST('2020-06-16' AS DATE);
I've got it, I used SELECT * FROM data WHERE date(date) BETWEEN '2020-06-01' AND '2020-06-16'; if someone's look for this too, please try this out :)

I want to find the previous month's date for specific date in SQL

I want to get the previous month date for specific dates in SQL. For example: 6.21.19 has a previous month date of 5.21.19.
I am just trying to get comps from this.
MONTH( curdate() ) -1
I need to return the previous month date.
Welcome to the board Arie. Judging from your question and responses, you need a range of dates and their prior month relations. The easiest way would be for all of the dates you need to look up to be in a table, then the answers provided so far would work. Since that doesn't appear to be the case, I'm guessing you are creating date ranges on the fly.
So lets assume you need exactly the data shown in your example, there are two parts to this, first you need to get a list of days that you want to look up, then you need to get the day in the prior month. There are lots of ways to get a sequence of days, but for simplicity I'll use a recursive CTE. Once I have the date range, I'll just select the dates and their prior month date as well.
with Date_CTE as (select cast('6/1/2019' as datetime) as repDate
union all
select dateadd(day, 1, repdate) as repDate
from Date_CTE
where repDate < '06/07/2019')
select repDate, dateadd(month, -1, repDate) as PriorDate
from Date_CTE
CTEs are helpful functions and you can get more details on them here, but it's worth noting there are many ways to do this. Hope this gets you pointed in the right direction.
SELECT yourDateColumn, yourDateColumn-interval 1 month as prevMonthDate

PURE SQL get days beteen given date and current date, without functions

I need to get number of days between 2 dates, a given one and current date.
But in pure SQL, I mean without usign functions, it is possible?
For exaple
SELECT days (t.givenDate) - days (current date) FROM table t
Have you any idea?
Thaks a lot.
The built-in function is datediff(). The equivalent for the above is:
SELECT datediff(t.givenDate, curdate()) FROM table t;
Normally, givenDate would be in the past and you would want the arguments in the other order.

mysql timediff no proper output when endtime is 24hrs

I'm using datatype time to calculate the class taken timings. For calculation I use TIMEDIFF(endtime,starttime).
Query
SELECT TIMEDIFF('00:26:08','21:58:18') FROM students_session WHERE id='#'
I'm not getting the proper o/p which is 02:27:50. Instead I get -21:32:10, which is wrong.
How to rectify this?
The issue is that you know that '00:26:08' is after '21:58:18' (following morning), but MySQL is not aware, thus the result is correct from MySQL point of view.
You either need to provide a date part, where the end_date falls to the next day, or you need to add 24 hours (1 day) to the end_date. These will tell MySQL that the end_date is greater than the start_date and you will get the results you expect.
SELECT TIMEDIFF(timeadd('00:26:08','24:00:00'),'21:58:18') from students_session where id='#'