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
Related
I want to check if a dataset is older than the current month -1 day (so if it's the first of November it should still be older than October). This is my SQL:
SELECT *
FROM XY
WHERE DATE_FORMAT(calendar_day, '%Y-%m') <> DATE_FORMAT((CURRENT_DATE()-1, '%Y-%m');
But it doesn't work because of the second DATE_FORMAT. If I remove it, it works, but then it also compares the days and not the months. How do I solve this?
I want to check if a dataset is older than the current month -1
Don't use DATE_FORMAT() on a column for this type of query. Keep all date functions on the "current date". Functions on columns impede optimization.
I think this does what you want:
SELECT *
FROM XY
WHERE calendar_day <= LAST_DAY(CURRENT_DATE() - interval 1 day - interval 1 month);
Try this using year and month function:
SELECT *
FROM XY
WHERE (year(calendar_day) <> year(CURRENT_DATE()-1))
and (month(calendar_day)<>month(CURRENT_DATE()-1))
In my database, I have a column with a check-in date and a column with a check-out date. I need to select every row that has a check-in date <= 7/30/2017 and a check-out date that is >= 7/30/2017.
This is the code I have now:
SELECT *
FROM `v_reservation_records`
WHERE cast(checkin as date) <= '7/30/2017'
AND cast(checkout as date) >= '7/30/2017'
Here is an example date from the DB:
2018-09-18
When I run this query, I do not get any results, but I know that I have a check-in date equal to 7/30/2017. What am I missing? Or is there an easier way to accomplish my goal?
Assuming that you are casting valid values for date
You should convert also the literal the date properly
SELECT *
FROM `v_reservation_records`
WHERE cast(checkin as date) <= str_to_date('7/30/2017' , '%d/%m/%Y')
AND cast(checkout as date) >= str_to_date('7/30/2017' , '%d/%m/%Y')
and you can also use between
SELECT *
FROM `v_reservation_records`
WHERE str_to_date('7/30/2017','%d/%m/%Y')
between cast(checkin as date) AND cast(checkout as date)
Try like this
SELECT *
FROM `v_reservation_records`
WHERE DATE_FORMAT(checkin, '%m/%d/%Y') between '7/30/2017'
AND '7/30/2017'
The dates in my database are stored as varchars instead of date formats due to the way it was first built.
The dates look like this:
e.g. 1/3/2015 and
10/3/2015
I'm trying:
"SELECT COUNT(*) n FROM tracker WHERE TIMESTAMP(STR_TO_DATE(date, '%d/%m/%Y'))<=NOW()"
However, that's not working. It is returning the count of all records, regardless of the date.
How can I count only the records where the date is today or in the past?
You do not need TIMESTAMP():
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= NOW()
You should pay attention to the functions STR_TO_DATE and NOW(), the first return a date, the second is a timestamp.
When you convert STR_TO_DATE(date, '%d/%m/%Y') you will get a date with hours, minutes and seconds as 00:00:00
Using CURRENT_DATE perhaps will match more closely the original requirements
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= CURRENT_DATE
Also I suggest you to rename the column 'date'
I put together a query for determining whether the time difference between the current date and a record from a database is exactly a month or more apart. I am comparing now() to a created_at column, which is a timestamp.
EX:
6-12-2014,
7-12-2014
AND
5-12-2014,
7-12-2014
Should be considered to be a desirable results.
SELECT count(*) FROM `subscriptions` WHERE
DATE_ADD(CAST(created_at as DATE),INTERVAL TIMESTAMPDIFF(MONTH, created_at, now()) MONTH) = CAST(now() as DATE);
However the query appears to not return all desired results. It returns 2-28-2014 and 7-28-2014, however it does not pull up 6-28-2014. Is there a better way of doing this than the solution I came up with?
Are you looking to count dates that are on the same day of the month as the current date? If so, try the DAYOFMONTH function:
SELECT COUNT(*)
FROM subscriptions
WHERE DAYOFMONTH(created_at) = DAYOFMONTH(NOW())
How can I get the rows in a table where today's date is between (inclusive) two DATE columns of that row?
For example, take these two columns of a table:
How could I get the first and second rows on the 10th of April, or the 3rd row on the 25th (inclusive, like I said)?
Any help would be greatly appreciated. Thanks in advance!
You can add a condition as follows
DATE(NOW()) between date1 and date2
You will find a lot of people using between operator, but I prefer using a simple AND operator.
I do that because although the between operator IS inclusive, simple dates (2012-04-10) can be counted as being midnight, and will thus not be inclusive.
So this should work just fine and will always include the boundaries of the date range:
SELECT * FROM table WHERE from_date <= '2012-04-10' AND to_date >= '2012-04-10'
Just use the SQL now() function to compare the date columns like so:
SELECT * from table where now() >= from_date and now() <= to_date
If you have date (not datetime) columns, use CURTIME() or DATE(NOW()), never NOW() as CesarC correct wrote and you can use BETWEEN.
SELECT * FROM table WHERE CURTIME() BETWEEN from_date AND to_date