Results 1 to 5 of 5

Thread: Infernal date handling in SQL !!!!

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Posts
    63

    Angry

    I have a date/time field in my access database (ie stored like 06/07/00 12:00:00) and I wish to do an SQL to find certain dates (ie. ignoring the time part of the data) I'm doing this:

    sSqlTxt="SELECT * FROM Letters WHERE DateSent = #" & Format(dtFrom.Value, "DD-MMM-YYYY") & "#"

    There are many records that match this, but this sql is not picking up anything. I think is something to do with the fact that it is comparing a Date/Time with just a date.
    I need the data in the database to contain the time with the date, but as far as this particular SQL goes, I just want to compare dates.

    Anyone know where am I going wrong ?
    Why is date handling within databases SO FIDDLY ?????


  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    I would change the Format part to "mm/dd/yyyy" and see if you get the results you expect.
    "It's cold gin time again ..."

    Check out my website here.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2000
    Posts
    63
    No, this doesn't work. There must be a way of doing this !!

  4. #4
    Fanatic Member Gary.Lowe's Avatar
    Join Date
    May 2000
    Location
    In my sphere of influence
    Posts
    621
    Hawker as Brucy said change the format to mm/dd/yyyy.
    SQL seems to prefer it in this manner.

    If you have access when you create a query with dates in them i.e. #01/02/2000#, you you can see how the SQL in SQl view changes the format to #02/01/2000#.
    Gary Lowe
    VB6 (Enterprise) SP5
    ADO 2.6
    SQL Server 7 SP3

    OK I know my spelling and grammer is crap so don't quote me on it!

    To err is human to take the P! is only natural !!

    Click on the top section of image for Marcus Miller website and bottom section of image for 'Run For Cover' sound clip


  5. #5
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    Think about your SQL statement for a minute. Is it surprising no records match midnight for the date you ask for?

    Try something like this, instead:
    Code:
    Dim sql_str As String, dt1 As String, dt2 As String
      dt1 = Format(dtFrom.Value, "#DD/MM/YYYY# ") 
      dt2 = Format(DateAdd(d, 1, dtFrom.Value), "#DD/MM/YYYY#") 
    
    sql_str = "SELECT * FROM Letters " & _ 
              "WHERE DateSent > " & dt1  & _
              "AND DateSent < " & dt2

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