I have 3 different dates and I'm trying to code what the closest upcoming date is. If the date is in the past it needs to be ignored. Also, date1 <= date2 <= date3
I've tried the following code with (8/18/2021, 8/19/2021, 8/20/2021) as an inputs, but the function returns 12:00:00AM not 8/18/2021
Public Function UpcomingDates(date1 As Date, date2 As Date, date3 As Date) As Date
Dim ClosestDate As Date
If date1 >= Date Then 'diff1 >= 0
ClosestDate = date1
ElseIf date2 >= Date Then
ClosestDate = date2
ElseIf date2 >= Date Then
ClosestDate = date3
End If
End Function
Why doesn't the function return 8/18/2021?
*I'm using VBA in MS Access Query
Dates are stored in Access (and other office products) as serial numbers. For example, "1/1/2008" is actually stored as 39448. The number 39448 is the number of days since Jan 1, 1900. Decimals of this number represent the time of day. So a data value with no decimal is assumed to be 12:00:00 AM. For example, 39448.25 would be 1/1/2008 6am, 39448.5 would be 1/1/2008 12pm, etc.
To have a function display the date only, use the format function:
Format(ClosestDate, "m/d/yyyy")
Related
I have three variables
1. Start Date (column in a table)
2. Current Date
3. Start Date + 1 week
I want to write a where clause in sql query in such a way that
Current Date should be between Start Date and (Start Date + 1 week)
and Current Date can be less than Start Date but
not greater than (Start Date + 1 week )
use the between function:
where now() between startDate and endDate
The logic of
Current Date should be between Start Date and (Start Date + 1 week)
and
Current Date can be less than Start Date but not greater than (Start Date + 1 week )
is conflicting, since if currentDate should be between startDate and startDate+ 1week, it will never be less than startDate
If you mean currentDate should be between startDate and startDate+1week write
WHERE DATE(NOW()) BETWEEN `startDate` AND DATE_ADD(`startDate`, INTERVAL 1 WEEK)
If you mean currentDate should be less than startDate+1week write
WHERE DATE(NOW()) <= DATE_ADD(`startDate`, INTERVAL 1 WEEK)
I have a table that has date and hours and i am looking to get in my output > > all with a particular date and hour and minutes of that day.
A particular person on the table can have a date_added of 10/12/2016 9:38:04 PM
I want to pull all who have a date added of 10/12/2016 after 9:25:00PM. I have tried different things but nothing worked, see below
s.date_added >= '2016-12-10 9:25:04 PM'
s.date_added >= '12/10/2016 09:00:00 AM'
convert(s.date_added) >= '10/12/2016 9:25:04 PM'.
I am using sql. I just want a code that will give me all those on a table that have a specific date added on the date added column. I am not trying to format, just get an output
set date to variable datetime before
declare #dateParam datetime
set #dateParam ='2016-12-10 9:25:04 PM'
s.date_added >= #dateParam
s.date_added >= #dateParam
example comparison with just hour
if datepart(hh, #dateParam) >= datepart(hh, s.date_added)
print 'True'
else
print 'False'
I am trying to select the current date based on the weeknumber in the weekstu table.
so lets says we are in week 1 which started Monday the 25/11/2013. I am trying to get that date based on using today date which is the 28/11/2013. Week 2 will be the Monday the 2/12/2013. The query I have below doesn't return any record. Is this possible to get the beginning of the week based on today date?
table:weekstu
weekid
startdate
setid
weeknumber
startdate
Table:week
weekid
setid
SELECT * from weekstu ws
JOIN week w ON ws.setid = w.setid AND ws.weekid ON w.weekid
WHERE ws.weeknumber = `1` AND startdate = CURRENT_TIMESTAMP
You can determine the beginning of the week like:
select adddate(curdate(), interval 1 - dayofweek(curdate()) day)
Note that a condition like:
startdate = CURRENT_TIMESTAMP
Will only match rows that have the exact current timestamp up to the millisecond. Unlike curdate(), current_timestamp contains time information as well as date information.
DATE_FORMAT(date,format)
%b Abbreviated month name (Jan..Dec)
%W Weekday name (Sunday..Saturday)
DATE_FORMAT(NOW(),'%W')
REF: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
is there a function in vb.net or mysql that checks if date2 is the date for tomorrow of date1?
like
date1 = 6/4/2013
date2 = 6/5/2013
is date2 = tomorrow of date1? and also the vice versa
id date1 = yesterday of date2?
Is there any function for checking that in mysql and vb? or it is entirely logic?
You just do a comparison:
where date(date2) = date(date1) + interval 1 day
The date() function gets rid of any time element that might cause the equality to fail.
I'm trying to write a query that will check today's date against my table columns date1 and date2 in mysql/php.. This is what I'm after:
'events' table:
date1 = start date (XXXX-XX-XX)
date2 = end date (XXXX-XX-XX)
query:
select * from events where 2012-01-18 between date1 and date2 (or equal to date1 and date2)
But I'm not sure how to go about it.. any help would be appreciated :)
EDIT:
Maybe I was unclear.. if todays date = '2012-01-18' I need it to find results if today's date is between the date range of date1 and date2.. So date1 may be '2012-01-04' and date2 may be '2012-01-21'.. so if todays date falls between or on those dates, a result is returned..
SELECT * FROM events
WHERE date1<='2012-01-18'
AND date2>='2012-01-18'
If your referring to compare the date today is between a start and end date, I think you should use this:
SELECT *
FROM table
WHERE '2014-08-20' >= start_date
AND '2014-08-20' <= end_date
Hope this helps :)
SELECT *
FROM events
WHERE date1 <= '2012-01-18'
AND date2 >= '2012-01-18';
This should get you started. You can use DATE(NOW()) to get today's date if you don't want to hardcode a date.
Try this,
SELECT * FROM events
WHERE date1<='2012-01-19'
AND date2>='2012-01-18'
Though there are many answers available to this question I would like to give my response regarding the same situation I faced.
I am using php to query mysql database.
first what I do is to convert the date in the mysql supported date format which is yyyy-mm-dd
$date = new DateTime($date);
$date=date_format($date, 'Y-m-d');
in the query's where clause I use BETWEEN
"'$date' BETWEEN start_date AND end_date"
this works perfectly given the case described here.
Modified version of styfle's code
$date1 = '2010-01-01'; // example min
$date2 = '2015-01-01'; // example max
$sql = "SELECT * FROM events WHERE $date1 >= '2012-01-18' AND $date2 <= '2012-01-18';";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) != 0) {
// This date is equal to or within the range specified
} else {
// The date was not within the range specified
}
then you can have code executed based on the result