vb.net get database data between two date - mysql

I have two datetimepicker, startDate is stored datetimepicker1 value and endDate is stored datetimepicker2 value.
I want to to get the data between startDate and endDate from database.
Dim bSql As String = "select date, sum(total_price) from bill where Date = '" & Format(startDate, "yyyy/MM/dd") & " and Date='" & Format(endDate, "yyyy/MM/dd") & "'"
i tried the code above but it can't work. Anyone can help me?

If you're trying to find a string format for a date at all, you've already lost. Try this:
Dim bSql As String = "select date, sum(total_price) from bill where Date >= #startDate and Date < #endDate;"
Using cn As New MySqlConnection("connection string here"), _
cmd As New MySqlCommand(bSql, cn)
cmd.Parameters.Add("#startDate", SqlDbType.DateTime).Value = startDate
cmd.Parameters.Add("#endDate", SqlDbType.DateTime).Value = endDate.AddDays(1)
cn.Open()
'...
End Using
No formatting required or wanted.

Try this, using the SQL BETWEEN operator, which allows you to specify the lower and upper bounds of a range.
Dim bSql As String = "select date, sum(total_price) from bill where Date BETWEEN '" & startDate.ToString("yyyy/MM/dd") & "' AND '" & endDate.ToString("yyyy/MM/dd") & "' GROUP BY date;"
You will also need to apply a grouping to use the aggregate function "SUM":
-- find all dates with sales and the total prices on each date
SELECT [date], SUM(total_price) AS [TotalPrice]
FROM bill
WHERE [date] BETWEEN '2013-01-01' AND '2013-12-31' -- use appropriate date format here
GROUP BY [date];

in data base i have DD/MM/YYYY
solution is to make in VB MM/DD/YYYY
this is a CODE
oldbc.CommandText = "select * from recette where Date between #10/09/2015# and #10/011/2015#"

Try formatting your dates like this (you will need to use the Value of the DateTimePicker as well):
Format(startDate.Value, "yyyy-MM-dd")
A better option is to use a parameterised query and then you don't have to format the date into any particular format. More info here: How do I create a parameterized SQL query? Why Should I?

Dim bSql As String = "select date, sum(total_price) from bill where Date = " & DateTimePicker1.Text & " and Date=" & DateTimePicker1.Text & ""
Set the datetime picker date format.
I hope it will helpful for you...

Another possibility is to make a little function to format DATE variable and return correct date string syntax for MySQL.
Public Function MySQLDate(inDate As Date) As String
Return "'" & inDate.ToString(format:="yyyy'/'MM'/'dd") & "'"
End Function
Now your query is more readable, easier to create, and looks like this:
Dim bSql As String = "select date, sum(total_price) from bill where Date BETWEEN " & MySQLDate(startDate) & " and " & MySQLDate(endDate)

Related

MSAccess VBA, filtering query results via combo box (text)

I want to filter date values (text) in a query based on a comboBox text value.
Here is what I have:
ComboBoxTimePeriod (ID [num], TimePeriod [text]).
In ComboBox I select Time period, like "16.10.2017-15.11.2017".
In a query I have Date field with single date, like "20.10.2017" (text).
What I want is to write an SQL code which searches for all records with date within TimePeriod range.
So far Idea is to extract SatrtDate and EndDate from TimePeriod like this:
Dim strStartDate As String
Dim strEndDate As String
strStartDate = Mid(Me.cboTimePeriod.Text, 1, 10)
strEndDate = Mid(Me.cboTimePeriod.Text, 12, 10)
Now from TimePeriod "16.10.2017-15.11.2017" I have StartDate (16.10.2017) and EndDate (15.11.2017).
And I want to filter all records which have date within those two dates.
This is where I need you guys.
Note that this is different issue from my last question, where I searched for a records with TimePeriodID (Subform filtering based off multiple parameters (Combobox AND Textbox)).
Always handle dates as Date, not text:
Dim StartDate As Date
Dim EndDate As Date
StartDate = DateValue(Split(Me.cboTimePeriod.Value, "-")(0))
EndDate = DateValue(Split(Me.cboTimePeriod.Value, "-")(1))
The build your filter:
... " Between #" & Format(StartDate, "yyyy\/mm\/dd") & "# And #" & Format(EbdDate, "yyyy\/mm\/dd") & "#"
To filter a form:
Me.Filter = "[Date Field] Between #" & Format(StartDate, "yyyy\/mm\/dd") & "# And #" & Format(EbdDate, "yyyy\/mm\/dd") & "#"
Me.FilterOn = True
And do make sure that your Date Field is of data type Date, not Text.

VB mysql date search is not working

Hello I am using VB6 and mysql and I am facing a problem in searching through dates its not fetching records
Here is my code
Dim GetDateNow As Date
Dim GetDateTen As String
Dim SetDateTen As Date
GetDateNow = Format$(Now, "yyyy-mm-dd")
GetDateTen = Now - 15
SetDateTen = Format$(GetDateTen, "yyyy-mm-dd")
Dim rs As New Recordset
SQL = "SELECT * FROM CreditPayLog WHERE payment_user='" & Label5.Caption & "' AND payment_date BETWEEN '" & Format(SetDateTen, "yyyy-MM-dd") & "' AND '" & Format(GetDateNow, "yyyy-MM-dd") & "'"
Also i have used debug.print function to see the query all looks good but its not fetching data here is the query
SELECT * FROM CreditPayLog WHERE payment_user='1222150322' AND payment_date BETWEEN '2016-04-06' AND '2016-04-21'
payment_date is DATETIME column in mysql
Your sql is creating a text/string value for date... you need to cast as a date. Not sure how in mySQL but in MSSql you would use Convert(datetime, 'yourDateAsString', 103)

Cant Figure out a good way to query a datatable in vb.net

So I'm making an application that gets all the events for a month out of a mysql DB and adds them to a calendar. I've got the events in a data table atm "dbTable"
The events are ascending by date
"SELECT * FROM table_events WHERE date BETWEEN '" & startDate & "' AND '" & endDate & "' ORDER BY date ASC"
Now I need to query each day one at a time to check for up to 6 events per date, any suggestions? Im not even sure on how to query the data table let alone do it for up to 31 days and make it somewhat efficient.
so after trying and reading i've figured it out. Thanks to all of you for your input.
Dim count As Integer
Dim expression As String
Dim foundRows() As DataRow
count = 0
expression = "date = '" & numericYear & "-" & doubleMonth & "-01'"
foundRows = dbTable.Select(expression)
For count = 0 To foundRows.Length - 1
'count = count + "1"
MessageBox.Show(dbTable.Rows(count).Item(5).ToString())
Next count
Where in this example numericYear = 2015 and doubleMonth = 06

how to make query by date in access 2013?

I made access application and i have query build on date criteria
this is my query
Set sales = CurrentDb.OpenRecordset("Select * From sales where action_date = #" & date_actions & "#")
I change the date in windows to dd/mm/yyyy
But when i try to run this query nothing happens
But when i change it to default MM/d/yyyy
It run correctly
How to solve this problem? Please, and thanks in advance
I suggest use this format: YYYY-MM-DD HH:MM:SS
You can convert your date as follows:
Format(date_actions, "yyyy-mm-dd hh:mm:ss")
Then your statement will be:
Set ftm_date = Format(date_actions, "yyyy-mm-dd hh:mm:ss")
Set sales = CurrentDb.OpenRecordset("Select * From sales where action_date = #" & ftm_date & "#")
JET engine deals date in American format, not the regular DD/MM/YYYY HH:NN:SS. So you need to format the dates accordingly.
Set sales = CurrentDb.OpenRecordset("SELECT * " & _
"FROM " & _
"sales " & _
"WHERE action_date = " & Format(date_actions, "\#mm\/dd\/yyyy\#"))
Hope this helps.

SELECT string date rows In a Date Range using variable

Good day! I have a problem about filtering string date in date range using variable. Here's my sample query that didn't work:
Select * from table_name where datee >= '" & result & "' and datee1 <= '" & result1 & "';
This code displays same date , 2012-12-31 and 2012-12-31.. What I want is on the greater than equal will display is 2012-12-01 and for less than or equal to is 2012-12-31.. Here's my code for date range:
Dim datee, datee1, result, result1 as string
Dim dateTime As Date
Dim dTime As Date
dateTime = Date.Parse(datee)
dTime = Date.Parse(datee1)
result = dateTime.ToString("yyyy-MM-dd")
result1 = dTime.ToString("yyyy-MM-dd")
Try this please: (assuming your variables are holding proper date formats and values and database string date format is YYYY-MM-DD)
Select * from table_name where Str_to_Date(datee,'%Y-%m-%d') >=
'" & result & "' and Str_to_Date(datee1,'%Y-%m-%d') <= '" & result1 & "'
;
Since 12-01 to 12-31 covers Month of December.. you can try:
Select * from table_name where Month(Str_to_Date(datee,'%Y-%m-%d')) = 12
;
Hence,
Select * from table_name where
Month(Str_to_Date(datee,'%Y-%m-%d')) = " & Month(result1) & "
;
Check the demo very carefully on the query that I have used month function and the string dates format in the sample demo table:
SQLFIDDLE MYSQL DEMO