Results 1 to 5 of 5

Thread: not really VB more of a SQL question

  1. #1
    Guest
    I have a database with the date field in it and I was wondering how i could use the select statement to get dates from a certain range(to be simply from one month). I tried using SELECT...WHERE thedates >= 1-1-1980 and thedates < 2-1-1980

    but it didnt seem to work. Any suggestions?

    thanks

  2. #2
    Lively Member
    Join Date
    Jul 2000
    Posts
    104
    You have to put the dates between # signs in your SQL statement.

    Or you could always use a parameterized query in access. Here is an example that I have in an app I developed:

    SQL Query:
    Code:
    PARAMETERS [Beginning date] DateTime, [Ending date] DateTime;
    SELECT Status, Ticket_Number
    FROM Ticket_Record
    WHERE Ticket_Record.Date >= [Beginning date]
    AND Ticket_Record.Date <= [Ending date] AND Ticket_Record.Status = "Referred";
    VB code to pass in the parameters and run the query (I was using DAO in the example):
    Code:
    Function GetWeekly(sQueryName As String, sDateBegin As String, sDateEnd As String) As Integer
    
        Dim qdf As QueryDef
        
        Set qdf = db.QueryDefs(sQueryName)
        
        qdf("Beginning date") = sDateBegin
        qdf("Ending date") = sDateEnd
        
        Set rs = qdf.OpenRecordset(dbOpenSnapshot)
        
            If rs.RecordCount = 0 Then
                GetWeekly = 0
            Else
                rs.MoveLast
                GetWeekly = CInt(rs.RecordCount)
            End If
            
        rs.Close
        
        Set rs = Nothing
        Set qdf = Nothing
        
    End Function
    Hope this helps.



  3. #3
    Lively Member
    Join Date
    May 1999
    Location
    KC
    Posts
    72
    Make sure you're using the correct date format, probably like 1/1/1980 and 2/1/1980 (slash instead of hyphen). And I think dates are enclosed in #'s, like this:

    SELECT...WHERE thedates >= #1/1/1980# and thedates < #2/1/1980#

  4. #4
    Guest
    You could also use "convert" to make sure the correct format is used.
    The "between" operator is another option instead of using the >= and <= operators.

  5. #5
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    You haven't indicated which DBMS your database is using, the suggestions by the other posters work for Access but won't for SQL Server (it wants your dates wrapped in quotes).

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