I have two datefield drop downs that the end-user will select their start and end dates from. However, when I run the query I'm missing the first date, i.e - I select the 9th-15th but I get the 10th-15th records.
My thinking was to somehow get just the day from the dropdown menu and manually hard code the start and end times (00:00:00 and 24:00:00)
I think I might be able to do it similar to this, but my code is not working.
Private Sub FindItem2()
SQL2.AddParam("@datestart", dateStart.Value.ToShortDateString + '00:00:00')
SQL2.AddParam("@dateend", dateEnd.Value.ToShortDateString + '24:00:00')
LoadGrid("SELECT SUBSTRING(CustomerNumber, PATINDEX('%[^0]%', CustomerNumber+'.'), LEN(CustomerNumber)),'D','CAFET','', sum(Total) as totalsales FROM ViewDetailedSalesReport WHERE CustomerNumber is not null AND DateSold BETWEEN (@datestart) AND (@dateend) GROUP BY CustomerNumber, CustomerLastName ORDER BY CustomerLastName ASC; ")
End Sub
EDIT NEW:
I deleted my code rather than trying to modify it. I used Cetin's code and tweaked it by moving the variables to the top, removing the tags, and adding quotes around the Query. After that it works, and exactly as it should.
Private Sub FindItem2()
SQL2.AddParam("@datestart", dateStart2.Value.Date)
SQL2.AddParam("@dateend", dateEnd2.Value.Date.AddDays(1))
Dim cmd As String = "SELECT SUBSTRING(CustomerNumber, PATINDEX('%[^0]%', CustomerNumber+'.'),
LEN(CustomerNumber)),
'D','CAFET','',
sum(Total) as totalsales
FROM ViewDetailedSalesReport
WHERE CustomerNumber is not null AND
DateSold >= @datestart AND DateSold <@dateend
GROUP BY CustomerNumber, CustomerLastName
ORDER BY CustomerLastName ASC;"
LoadGrid(cmd)
End Sub
EDIT-OLD: I got the query to work "correctly" however I'm not sure this is the best practice way to accomplish this task.
Private Sub FindItem2()
SQL2.AddParam("@datestart", dateStart2.Value)
SQL2.AddParam("@dateend", dateEnd2.Value)
LoadGrid("SELECT SUBSTRING(CustomerNumber, PATINDEX('%[^0]%', CustomerNumber+'.'), LEN(CustomerNumber)),'D','CAFET','', sum(Total) as totalsales FROM ViewDetailedSalesReport WHERE DateSold >= DateAdd(day,-1,@datestart) AND DateSold < (@dateend) GROUP BY CustomerNumber, CustomerLastName ORDER BY CustomerLastName ASC; ")
End Sub
I modified the section as suggested by Cetin using >= and < rather than BETWEEN, and changed the dayoffset by -1 using DateAdd.
DateSold >= DateAdd(day,-1,@datestart) AND DateSold < (@dateend)
ViewDetailedSalesReport.DateSold? If it's aDATETIME, do the values in the table actually have a time, or are the times all zero'd out? Same questions apply to thedatestartanddateendvariables in your script. Are theyDATES,DATETIMEs orDATETIMEs with zero'd times?