Results 1 to 2 of 2

Thread: Having trouble with a DateTime in my query

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Having trouble with a DateTime in my query

    I am trying to grab the purchase orders from my database that took place on todays date.

    My query looks like this:
    VB Code:
    1. Dim cnn As New SqlConnection(strCon)
    2.         Dim cmd As New SqlCommand
    3.         Dim da As New SqlDataAdapter
    4.         Dim ds As New DataSet
    5.         'Try
    6.         cmd = cnn.CreateCommand
    7.         cmd.CommandText = "SELECT " & _
    8.             "PurchaseOrders.CreationDateTime," & _
    9.             "PurchaseOrders.StoreID," & _
    10.             "PurchaseOrders.PurchaseOrderNumber," & _
    11.             "PurchaseOrders.VendorCode," & _
    12.             "PurchaseOrderItems.ItemCode," & _
    13.             "PurchaseOrderItems.Description," & _
    14.             "PurchaseOrderItems.VendorItemCode," & _
    15.             "PurchaseOrderItems.VendorUnitCode," & _
    16.             "PurchaseOrderItems.VendorOrderQuantity," & _
    17.             "Stores.AddressLine1, Stores.City," & _
    18.             "Stores.State, Stores.ZipCode " & _
    19.             "FROM PurchaseOrderItems INNER JOIN " & _
    20.             "PurchaseOrders ON PurchaseOrderItems.PurchaseOrderID = " & _
    21.             "PurchaseOrders.PurchaseOrderID INNER JOIN " & _
    22.             "Stores ON PurchaseOrders.StoreID = Stores.StoreID " & _
    23.             "WHERE PurchaseOrders.PurchaseOrderStatusCode = 'Ordered' AND (" & _
    24.             "PurchaseOrders.VendorCode = 'Spartan Oil Corporation' OR " & _
    25.             "PurchaseOrders.VendorCode = 'Vesco Oil Corporation' OR " & _
    26.             "PurchaseOrders.VendorCode = 'Polymer Dynamics' OR " & _
    27.             "PurchaseOrders.VendorCode = 'Mighty Auto Parts East') AND " & _
    28.             "PurchaseOrders.CreationDateTime >= " & "2/19/2007 10:21:00 AM"
    29.         da.SelectCommand = cmd
    30.         da.Fill(ds, "Orders")
    31.         dgvOrders.DataSource = ds
    32.         dgvOrders.DataMember = "Orders"
    In that huge query the last item in the Where Clause is what I am having trouble with. I just want to pull Purchase orders that were made on the current days date. The database is SQL Server and the field is a DateTime Field. I am sure it is the way I have set the date but that is how it is set up in the SQL table.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Having trouble with a DateTime in my query

    Firstly, how is this:
    VB Code:
    1. "PurchaseOrders.CreationDateTime >= " & "2/19/2007 10:21:00 AM"
    an improvement over this:
    VB Code:
    1. "PurchaseOrders.CreationDateTime >= 2/19/2007 10:21:00 AM"
    Secondly, enclose date/time values in "#" symbols. You can use single quotes too but I think it's preferable to use hash symbols to differentiate them from strings.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width