VB.NET How to create a reminder by checking database saved dates - mysql

I have this few line of codes where I save the date from datetimepicker to my database column REMINDER.
myCommand.CommandText = "Update kup_table SET REMINDER = #reminder Where ID = #theIDD"
myCommand.Parameters.AddWithValue("#reminder", DateTimePicker1.Value.Date)
myCommand.Parameters.AddWithValue("#theIDD", theID)
myCommand.ExecuteNonQuery()
In my database, the date is saved in this format 2015-12-14 00:00:00 since the datatype is DATETIME.
How do I compare it with the date now? If the saved date and today date are a match, then a reminder will go off.
I have tried using this sql command but having still having trouble where the reminder is always zero. Thanks in advance.
myCommand.CommandText = "Select COUNT(*) from kup_table Where REMINDER BETWEEN DATE() AND DATEADD('d', 1, DATE())"
mySqlConn.Open()
Console.WriteLine("Connected.")
count = myCommand.ExecuteScalar()
MsgBox("You have " + count.ToString + " reminder(s).")

Select COUNT(*) from kup_table Where DATE(REMINDER) = DATE(NOW())
The DATE() function returns the date part of the datetime only. (e.g. 2015-12-14)
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date

Related

SparkSQL between timestamp in where(filter) clause (V.S. In MySQL)

Describe:
I have a table with timestamp column and i want to get the number of values where the timestamp in specific time window.
My code is as shown in here:
String startTime = "2018-08-08 00:00:00";
String endTime = "2018-08-08 23:59:59";
productDF.where("CREATETIME >= '" + startTime + "' AND CREATETIME <= '" + endTime + "'").count();
I also tried between...and...sentence; and also:
productDF.where(unix_timestamp(col("CREATETIME"), "yyyy-mm-dd hh:mm:ss")
.cast("timestamp")
.between(
Timestamp.valueOf(startTime),
Timestamp.valueOf(endTime)
)).count();
The result i get is 6843.
But when i operate the sql sentence using Navicat:
SELECT COUNT(*) FROM my_table
WHERE CREATETIME BETWEEN '2018-08-08 00:00:00' and '2018-08-08 23:59:59';
it shows 7689.
Problem:
I want to know why i get the different results in Spark and Mysql.....what am i missing here??
Problem solved!
The problem happened because of the TIMEZONE.
In spark env., it get the timezone from_unixtime..So need to set the config.
.config("spark.sql.session.timeZone", "UTC")
But I still don't understand why the spark sql session flow the system timezone instead of just select from the column.....

calculation of expiration dates with sql

my first time doing a date calculation on my system
as you can see here on my first query how would i say that my expdate table is equal to this current date? then if so msgbox me "your item has expired"
on my second query i wanted to set a msgbox where msgbox me three months before my expdate ?
heres what i tried to do
cn.Open()
Dim query As String
query = "Select * from tblmeds where TIMESTAMPDIFF(MONTH,`expdate`,CURRENT_TIMESTAMP())< 1"
command = New MySqlCommand(query, cn)
readers = command.ExecuteReader
Dim count As Integer
count = 0
While readers.Read
count = count + 1
End While
cn.Close()
If count = 1 Then
msgbox "you have a expired items"
else
"no items are at risk"
PS:i am currently using PHPMYADMIN as my database
Assuming SQL SErver...
Subtract 3 months from the expiration date and compare that to the utc date (if multiple timezones are involved) otherwise you could just use getDate()
SELECT EXPDATE
FROM tblMeds
WHERE Dateadd(Month, -3,expDate) < = getutcdate()
If you change you query to
query = "Select count(*) as cnt from tblmeds where TIMESTAMPDIFF(MONTH,`expdate`,CURRENT_TIMESTAMP())< 1"
Then you won't need a look to count the rows, the server can do that which is faster.

vb.net with mysql how to pick between to dates of different years?

I have a vb.net program that queries a mysql database. i can search and get records between to dates with this code:
sqlQRY12 = "SELECT * from mating WHERE date BETWEEN '" & export_daily_date_DateTimePicker1.Text & "' AND '" & export_daily_date_DateTimePicker2.Text & "' AND chkbox = '0' ORDER BY lot_id ASC"
my format for my date is:
export_daily_date_DateTimePicker1.Format = DateTimePickerFormat.Custom
export_daily_date_DateTimePicker1.CustomFormat = "MM/dd/yy"
export_daily_date_DateTimePicker2.Format = DateTimePickerFormat.Custom
export_daily_date_DateTimePicker2.CustomFormat = "MM/dd/yy"
however if i try to search between two years like 12/20/13 - 02/20/14 I return no records when i know they exist? Any help would be great ty
Generally dates expressed as strings in database queries should be in the format "YYYY-MM-DD".
You essentially want your executed query to be this:
SELECT * from mating
WHERE date BETWEEN '2013-12-20' AND '2014-02-20'
So change the format of your dtpickers to be yyyy-mm-dd like this:
export_daily_date_DateTimePicker1.Format = DateTimePickerFormat.Custom
export_daily_date_DateTimePicker1.CustomFormat = "yyyy-MM-dd"
export_daily_date_DateTimePicker2.Format = DateTimePickerFormat.Custom
export_daily_date_DateTimePicker2.CustomFormat = "yyyy-MM-dd"
You should never concatenate values into your SQL commands. If at all possible, you should use parameters. With parameters, you can specify the value as it's actual type (Date) rather than as the string representation. The ADO Provider will handle converting the value correctly for you.
cmd.CommandText = "SELECT * from mating WHERE date BETWEEN #date1 AND #date2 AND chkbox = '0' ORDER BY lot_id ASC"
cmd.Parameters.AddWithValue("#date1", export_daily_date_DateTimePicker2.Value)
cmd.Parameters.AddWithValue("#date2", export_daily_date_DateTimePicker2.Value)

Insert a specific date in Mysql using vb.net

Okay here is the scenario, I need to insert a specific date in mysql. Everytime I insert this date I get 0000-00-00.
Everytime a user pays between the 1st and the 20th of the month then the wb_due-date column would increment the month by 1
Ex.
I have a default value of wb_paid-date = 2013-10-15 and wb_due-date = 2013-10-20.
Now User1 Paid on 2013-10-15 and after I clicked button, the date saved on wb_due-date was 0000-00-00 instead of 2013-11-20
Take a look at my code
Function iterate(ByVal d As Date) As String
Dim m As Integer = d.Month
If d.Month >= 1 And d.Month <= 11 Then
m += 1
ElseIf d.Month = 12 Then
m = 1
End If
Return m
End Function
cmd = New MySqlCommand("INSERT INTO tbl_billing(wb_paid-date, wb_due-date)
VALUES(CURDATE(), iterate(Now.Date) , con)
First, let's fix your function:
Function iterate(ByVal d As DateTime) As String
Return d.AddMonths(1).ToString("yyyy-MM-dd")
End Function
Also, if you're putting a string date into command, you're almost certainly doing something wrong. Let's just do this:
Function iterate(ByVal d As DateTime) As DateTime
Return d.AddMonths(1)
End Function
Then we'll fix your Sql Command:
cmd = New MySqlCommand("INSERT INTO tbl_billing(wb_paid-date, wb_due-date) VALUES(CURDATE(), ? " , con)
cmd.Parameters.Add("?", SqlDbType.DateTime).Value = iterate(Today)

how to change Access date variable's format

I have a user inputed date format of mm/dd/yyyy. I want to use this input in a query to act as a filter (start and end time). The actual date stored in the datebase was set with now(), so it has the format mm/dd/yyyy XX:XX:XX AM/PM.
How can I use these inputed dates in my filter? (When I tried out an mm/dd/yyyy input I was returned a Report with no values, but if I added the time, it worked, but I don't want the user to have to enter the time.)
Right now I am using simple input boxes for the input. I really would like to create a calendar popup (and I see tutorials online). Would the solution change if I changed over to a Calendar?
EDIT:
Here is my code, to make it eaiser to understand the issue.
Dim startDate As Date
Dim endDate As Date
startDateString = InputBox("Enter a start date.")
endDateString = InputBox("Please enter and end date.")
Reports![rpt_Inventory_Update].RecordSource = "MyQuery"
Reports![rpt_Inventory_Update].Filter = "Modification_Date BETWEEN #" & startDate & "# AND #" & endDate & "#"
Reports![rpt_Inventory_Update].FilterOn = True
When you compare to the field with Now(), you could change it to be compared to DateValue(Now())
Now() = Date() returns False
DateValue(Now()) = Date() returns True
I figured it out. This is my new code:
startTime = TimeValue("00:00:00")
endTime = TimeValue("23:59:59")
startDate = startDate + startTime
endDate = endDate + endTime
Reports![rpt_Inventory_Update].RecordSource = "MyQuery"
Reports![rpt_Inventory_Update].Filter = "Modification_Date BETWEEN #" & startDate & "# AND #" & endDate & "#"
Reports![rpt_Inventory_Update].FilterOn = True
This makes sure that the start and end dates in the query have a full time stamp, and thus the query works!