Filter DataTable by DateTime (ShortDate) - sql

I'm working on a VB.Net project in Visual Studio 2013, and I have a data table full of rows containing ShortDate format strings in a column called "Date Purchased". I want to filter the data such that all rows that pass through the filter are imported to another data table.
For example, if the minimum filter was "01/01/1750" and the maximum filter was "01/01/2050", a row containing "4/12/1989" in the appropriate column would be imported to a different table.
The filter looks something like this:
Dim dateMin As String = "01/01/1750"
dim dateMax As String = "01/01/2050"
Dim dateExpression As String = "Date Purchased >= #" & dateMin & "# and Date Purchased <= #" & DateTime.Parse(dateMax.Text) & "#"
Dim dateFilter() = oldTable.Select(dateExpression)
For Each r As DataRow In dateFilter
newTable.ImportRow(r)
Next
My problem is that I get the error message "Additional information: Syntax error: Missing operand after 'Purchased' operator." and no rows are imported. dateExpression looks something like this:
"Date Purchased >= #1/1/1700# and Date Purchased <= #12/31/3000#"
Why doesn't this work? Any help would be appreciated, thank you.
EDIT: The problem was completely resolved by using [Date Purchased] instead of "Date Purchased". Thanks!

The problem was resolved by using [Date Purchased] instead of "Date Purchased".

Related

String to date error (Conversion from string to type date is not valid)

I want to crate date as MS Access like date (example -> #mm/dd/yyy#) input date can be string or date so I have created object variable to hold value of date. Then I convert it to msaccess date.
but it gives error. See the picture attached.
Before posting this question I had searched a lot, but I don't understand the solutions I found.
A major solution is ParseExact("Date String", "Format", System. IFormatProvider). But in my editor Intellisense does not recognize culturevalue. It tells me culturevalue is not defined.
Then I tried Date.tryparse(). It worked, but if I have to use this, then what is use of CDate conversion function?
Edit 1 :
Date.tryparse() does not work it returns false
in vb6
"#" & Month(dateIn) & "/" & Day(dateIn) & "/" & Year(dateIn) & "#"
works perfectly
It seems that you ask for a string expression for a VBA date value.
That can be done like this:
Dim DateIn As DateTime
DateIn = DateTime.Today
Dim Rval As String
Rval = DateIn.ToString("'#'MM'/'dd'/'yyyy'#'")
Console.WriteLine(Rval)
' Output:
' #07/10/2022#

Format Field to Days with VBA or SQL

I am building a table in MS Access using SQL, one of the columns has a duration in days. I understand that if I build a new table with a Make Table Query, the column format properties just have to be set to #"days". Is there a way to do this in the code?
Dim tbl As String
tbl = "SELECT [Table1].[Task], [Table1].[Duration], [Table1].[Date] " & _
"INTO [" & all & "] " & _
"FROM [Table1]"
DoCmd.RunSQL tbl
When this code runs the new table converts Duration field to dates.
For Clarity: all is a variable name for the new table.
Thank you!!!
You can do with DAO. Open the TableDef and set the Format property of the duration field.
But you wouldn't have to. To view the data, create a query or form to view the data, and apply the format to the field or textbox displaying the duration.
Or create a query for the export:
Select *, Format([Duration], '0') & ' days' As Days
From YourTable
or even:
Select *, Format([Duration], '0') & ' day' & IIf([Duration] = 1, "", "s") As Days
From YourTable
Have you tried something like;
Sub Format_Dates()
Selection.NumberFormat = "[$-en-US]d-mmm-yy;#"
End Sub
"Selection" can be replaced by Range.NumberFormat.
(You would just need to find the date format you need. PowerSpreadsheets has a good article on this found here "Excel VBA Date Format")
I'd just store it as a number.
If you want to output it as a date, use the DateAdd("d", num-days, base-Date)

How to filter out birthdays based on birth months from a data table

I'd like to get the list of people, from a data table named Patient, filtered according to their birth month. The data table has a birthday column wherein the elements have the format of mm/dd/YYYY. Any ideas on how to do this?
I'm currently using the binding source method. I managed to filter it according to other fields, and I can't think of a way on how to do this one. Thank you
`Dim bs As New BindingSource
bs.DataSource = StAnnes.Patient
If cmbFilter.Text = "Name" Then
bs.Filter = "Patient LIKE '%" & txtboxFilter.Text & "%'"
ElseIf cmbFilter.Text = "Sex" Then
bs.Filter = "Sex LIKE '%" & txtboxFilter.Text & "%'"
ElseIf cmbFilter.Text = "Month of Birth" Then
bs.Filter = "'Format('Date of Birth','mmm') LIKE 'Jan'"
ElseIf cmbFilter.Text = "Address" Then
bs.Filter = "Address LIKE '%" & txtboxFilter.Text & "%'"
End If
dgvPtList.DataSource = bs`
There is no Format function available for the Filter expression.
You can turn around the problem using a conversion with probably horrible performances.
bs.Filter = "SUBSTRING(CONVERT([Date of Birth], 'System.String'), 1,2) = '01'");
Here we try to convert the datetime column to a string, then we take the first two characters of the converted string and apply the logical comparison to the value 01 representing January. Of course this should be tested against your locale settings. The conversion produces a string that I don't know how it is formatted. I assume that the first two chars are the Month, but you need to test it in your environment.
Do not forget to put a column that contains spaces inside square brackets.

Choosing specific dataview values from a SQL statement and assigning them a variable VB

Good morning! I am attempting to create a timelog for a current project, I need to somehow assign a value to the variable 'depart' from a dataview that I've pulled. I'm sorry if this is like another question but I couldn't seem to find something to help my answer. Please note: depart, firstDate, etc. are all declared already.
dataview = dba.Query("SELECT Time, Status FROM Timelog WHERE [Date] = '" & firstDate & "' AND USERNAME LIKE '" & AgentList.SelectedValue & "' ")
For Each rowView AS DataRowView in dataview
DIM row as DataRow = rowView.Row
If dataview.Find("Departed") Then
depart = dataview.Find("time"
End If
Next
I plan on using the DateDiff function to calculate the hours between a departure and return from lunch, and then lunch and arrival.
I have no idea what dba.Query() returns but I assume it is some type of collection, based on the fact you are iterating through it on the next line. Because of that, dataview.find() would NOT be returning a value from a row, but more like the index of the column that has that name.
What you need to do is get the value of the field for the current row of your iteration:
depart = rowView("Time")

VB.NET 2012 : Select rows from DataView querying with 'Date'

I have a table which contains a date column called PurchaseDate
I have a list box which displays the months. When I click a month , I need to query the dataSource and collect the rows which have the purchase date in the SelectedMonth.
dv2 = New DataView(ds.Tables(0), "PurchaseDate LIKE '" & SelectedMonth & "/%'", "BillNo", DataViewRowState.CurrentRows)
This code is not working. Because here PurchaseDate is in Date format like 'MM/DD/YYYY'. I think I need to convert the date into string before using LIKE operator. I also tried using as below. Even then, it didn't go fine.
dv1 = New DataView(ds.Tables(0), "convert(varchar2(20),PurchaseDate,103) LIKE '" & SelectedMonth & "/%'", "BillNo", DataViewRowState.CurrentRows)
Here SelectedMonth will be a string like '01', '10'..
Use Linq to avoid such issues:
Dim selectedMonth = Int32.Parse(lbMonth.Text)
Dim filteredRows = From r In ds.Tables(0)
Where r.Field(Of Date)("PurchaseDate").Month = selectedMonth
' if you need a new DataTable
Dim tblFiltered = filteredRows.CopyToDataTable()
You don't say what database you are using, so these are the culture independent date literals that I use in the same scenario...
Oracle
TO_DATE('18-Dec-2012','dd-Mon-yyyy')
SQL/Server
'18-Dec-2012'
Ms/Access (not culture independent, but it's the only thing MS/Access accepts)
#12/18/2012#
So you might format your SQL to say something like this...
PurchaseDate BETWEEN '1-Dec-2012' AND '31-Dec-2012'