curdate() till rest of the month mysql - mysql

I'm trying to retrieve data between today and the rest of this month.
select * from table where date between curdate() and endmonth();
My experience with mysql dates/times is pretty bad.

MySQL has a LAST_DAY function that returns the last day of the passed month. I'd use that:
SELECT * FROM table WHERE date BETWEEN CURDATE() AND LAST_DAY(CURDATE());

I'd go with a little trick with DATE_FORMAT()
select * from table where date between curdate() and DATE_FORMAT(CURDATE(), '%Y-%m-31');
This way you don't have to deal with complicated and year(...) = ... and month(... . It's just nice and easy to write.
Read more about it here.

SELECT * FROM table WHERE date BETWEEN CURDATE() AND LAST_DAY(CURDATE());

Related

Retrieve data of last month uploads

I'm trying to retrieve some data from a database using MySQL. I would like to SELECT just the records that I uploaded in the last month and not the previous ones. This script has to be dynamic : I mean that on 1st of February it should retrieve data from 1st of January to 1st of February and so on.
My table structure is really simple : the upload date is stored in the column 'reg_date' that is a TIMESTAMP type.
Does anyone know how to do this in a single query? Thanks!
This can all be done in a single SQL query using the CURDATE() and INTERVAL functions so no PHP date calculation is required.
SELECT *
FROM `TABLE`
WHERE `reg_date` BETWEEN CURDATE() - INTERVAL 1 MONTH AND CURDATE()
You can filter the data like so:
select *
from your_table
where reg_date between date_sub(curdate(), interval 1 month) and curdate()
just use this query:
select * from your_table_name where reg_date between '2017-03-01' and '2017-03-31';

Calculate difference between dates

The title might be a bit misleading, but what I want is:
SELECT * FROM table ORDER BY pid ASC
And in one of the columns I have a DATE(). I want to compare the current date (not time) and return how many days are left till that date. Let's say the date is 2013-04-20 and today's date is 2013-04-16 I don't want to get any data if it's < current date. If it is I want it returned in days.
I've been looking around here and I've found no way to do it, and I can't for the love of me figure it out.
If you're looking for the difference between two date you can use the GETDATE function in MS SQL
SELECT DATEDIFF(DD, DateOne, DateTwo) FROM TABLE
This will return the difference in number of days between the two dates.
If you only want rows where the date field is less than or equal to today's date you can use:
SELECT DATEDIFF(DD, DateField, GETDATE())
FROM TableName
WHERE DateField <= GETDATE()
If you're using MySQL you can use DATEDIFF()
SELECT
DATEDIFF(NOW(), date_column) AS days_diff
FROM
tablename
Get the difference between two dates (ANSI SQL)
select the_date_column - current_date as days_left
from the_table
where the_date_column - current_date <= 4;
SQLFiddle: http://sqlfiddle.com/#!12/3148d/1

Return all rows which are same day in MySQL

I store a date in my database as a string like this:
03/08/2013 --> 8th of march
I'm trying to select only the rows that are the same day as the current day:
SELECT * FROM wp_aerezona_booking WHERE DATE_SUB(CURDATE(),INTERVAL 1
DAY) <= STR_TO_DATE(date, '%m/%d/%Y')
The above is what I tried, but it is returning a lot of results and should only return 1.
This should work already:
SELECT * FROM wp_aerezona_booking
WHERE STR_TO_DATE('03/08/2013', '%m/%d/%Y') = CURDATE();
By using the DATE_SUB you are subtracting1 day from the current day. You're not looking at today but yesterday. Also the <= makes you look at yesterday and all days before that.
Then you don't want <=, but you want =. The former will get all results if date is less than or equal to yesterday's date. I'm not sure that you even want the DATE_SUB either.
If you want the same date as today's date then you have to use "=" operator with.
SELECT *
FROM wp_aerezona_booking
WHERE STR_TO_DATE(date, '%m/%d/%Y')= CURDATE()

Is there anyway to do math on a MySQL query?

I have two fields, one with the purchase date and one with the lifespan of an item. I am wondering if there is any way to determine if the current date is past the lifespan. So, pretty much if MySQL could test if current_date is > purchase_date + lifespan.
The purchase date is a date formated yyyy-mm-dd and lifespan is an integer of months. Is there any way to perform this calculation?
You might try:
SELECT * FROM table WHERE NOW() > (purchase_date + INTERVAL lifespan MONTH)
But you might use
SELECT * FROM table WHERE NOW() > date_add(purchase_date, INTERVAL lifespan MONTH)
Or DATEDIFF
SELECT * FROM table WHERE DATEDIFF(NOW(), purchase_date) / 30 < lifespan
How about using DATEDIFF?
Your query would be something along these lines:
SELECT * FROM table WHERE DATEDIFF(NOW(), purchase_date) < lifespan
Forgive me if my syntax is a little off, I don't have an SQL instance to test on right now
Yes it is possible, using the MySQL date and time functions.

Use MySQL CURDATE() and CURTIME() to return results up and coming but not passed

Users of my site will be able to create events. They will set a date and a time separately. The event will be saved to a mysql database containing separate date and time columns.
I would like to query the database to return events which are up and coming but not passed. i.e. the event date is today or in the future and if the event date is today check the the time of the event hasn't passed.
I don't know the syntax for this and I cant seem to find it anywhere.
If anyone knows a good way to do this I'd very much appreciate your help.
The simplest way to express this efficiently in SQL and make use of any indexes you have on the date and/or time columns would be a query like this:
SELECT *
FROM your_table
WHERE date_column > current_date()
OR (date_column = current_date() AND time_column > current_time())
Depending on which version of MySQL you are using and how your table is indexed there is a small chance that the optimizer would prefer the query expressed as two SELECTs UNIONed together to avoid the OR:
SELECT *
FROM your_table
WHERE date_column > current_date()
UNION
SELECT *
FROM your_table
WHERE date_column = current_date()
AND time_column > current_time()
select * from table where concat_ws(' ',date_field,time_field) > now()
Why don't you use a datetime field?
in pseudocode
if ((date > today) OR (date == today AND time > now))