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.
Related
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")
I want to UPDATE time only from datetime using sql but it seem cant work. the update will get from user and only change the time.
example 2018-10-06 08:00:00 update to 2018-10-06 12:00:00 (time that user enter)
$sql3="UPDATE course
SET date_start = '$date_start'
WHERE date_start = SUBSTRING(date_start,11,17)
AND CourseNo = '$id1' ";
$hasil3=mysql_query($sql3);
if($hasil3==false)
echo "SQL error:".mysql_error();
else
{
?> <center>
<script language="javascript">
alert("Successfully update!");window.location="studentTimetable.php";
</script>
You can use an expression such as:
select date('2018-10-06 08:00:00') + interval 12 hour
You can also use addtime() if you want to add hours/minutes/seconds.
This should be simple enough to put into an update statement if that is what you want to do.
If I understand you right, you get an input as string in the format <hours> ":" <minutes> ":" <seconds>, that represents a time of the day. You want to replace the time of the day portion of a datetime column with that given time of the day.
To do so you can first downcast the column to a date and then upcast it again to a datetime. That way the time of the day portion becomes 00:00:00. Now use date_add to add the time of the day the user has given to you. Since the time of the day was zeroed before that will result in a datetime with the time of the day as the user put in.
UPDATE elbat
SET nmuloc = date_add(cast(cast(nmuloc AS date) AS datetime), INTERVAL '12:00:00' HOUR_SECOND);
I had a similar problem to this..
I have a number of dates that are not quite right by a few seconds. They should all end on an Hour, i.e 00:00
Work out how much time I need add (I know i want to add some seconds)
SELECT 60 - DATEPART(SECOND, EndDate), e.*
FROM Events e
WHERE DatePart(SECOND, EndDate) <> 0
Now I can write an update to correct all the dates that are slightly off.
UPDATE Events
SET EndDate = DATEADD(SECOND, i.Seconds, i.EndDate)
FROM (
SELECT Id, 60 - DATEPART(SECOND, EndDate) AS Seconds, EndDate
FROM Events e
WHERE DatePart(SECOND, EndDate) <> 0
) i
WHERE
i.ID = Events.ID
Is there any way to find all records in MySql Query between 2 dates where we want to increment 2nd date by 1.
Suppose user inputs Date1 as 2017-04-01 and Date2 as 2017-04-25 but here I always want to take Date2 after incrementing it by 1 Day i.e. Date2 as 2017-04-26 here.
Please let me know if possible?
You can use MySQL's DATE_ADD() function to add one day to the upper date in your range:
SELECT *
FROM yourTable
WHERE date BETWEEN '2017-04-01' AND DATE_ADD('2017-04-25', INTERVAL 1 DAY)
I have a cloclin program where studnets are clocked in and out. If a student is not clocked out by 6:30 PM at the end of the day I have a mysql statement that executes and clocks the student out at 6:30pm automatically to help keep the tables clean.
When a student is clocked into the system it fills the start time with the proper time and the end time is left as "0000-00-00 00:00:00". I am having a hard time determining the sql statement to detect the emtpty EndTime field to determine if it is blank. Since by default the table lists the blank date field as 0000-00-00 00:00:00" the CONCAT method I use to update the field seems to work when updating the field, but does not work to determine the criteria "WHERE"
My current sql statement is
$sql = "UPDATE `daycare attendance table` SET EndTime = CONCAT('1899-12-30 ', '18:30:00') WHERE EndTime = CONCAT('0000-00-00 ', '00:00:00')";
I also tried
$sql = "UPDATE `daycare attendance table` SET EndTime = CONCAT('1899-12-30 ', '18:30:00') WHERE EndTime = ''";
and
$sql = "UPDATE `daycare attendance table` SET EndTime = CONCAT('1899-12-30 ', '18:30:00') WHERE EndTime IS NULL";
The script just errors out as
Warning: mysql_db_query(): supplied argument is not a valid MySQL-Link resource in...
any help?
Since it runs daily, you could just say where end_time < now() - INTERVAL 24 HOUR and start_time >= now() - INTERVAL 24 HOUR;
Ok, updated a sample fiddle for you using timestamp -> http://sqlfiddle.com/#!2/bbdfe/1
Probly the best thing to do is to change your default from '0000-00-00 00:00:00' which isn't valid for a timestamp field to a valid time -> 1970+ and all your problems will then go away.
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