Results 1 to 5 of 5

Thread: [RESOLVED] SQL Statment help (Date)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    Resolved [RESOLVED] SQL Statment help (Date)

    Greetings,

    i have a DB with Appointments.

    datestartand and dateend are fields containing the date from to.
    The 2 fields are of the format Timestamp.

    I am getting in the Application 2 dates (from - to) and need now to check if an appointment is allready boocked.

    So how can this Querry be look like? What ever i try i get the wrong result

    Code:
    strSQL = "SELECT Count(*) FROM [Table] WHERE [datestart] >= " & _
                 makeDBDatumSQL(dtEnde) & " and [dateend] <= " &        makeDBDatumSQL(dtStart) & " and lgppk = '" & strPK & "'"
    many thanks for your help in advance

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: SQL Statment help (Date)

    Assuming you're talking about SQLServer, Timestamps are not the same as datetimes. You can't do a comparison against them in the way that you have. To achieve the result you're desrcibing you're going to need datetime fields for start and end date.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    Re: SQL Statment help (Date)

    @FunkyDexter

    DB = PostGreSQL

    the function makeDBDatumSQL returns a correct DB timeformat

    SELECT Count(*) FROM [Table] WHERE [datestart] >= " & _
    makeDBDatumSQL(dtEnde) & " and [dateend] <= " &
    makeDBDatumSQL(dtStart) & " and lgppk = '" & strPK & "'"

    only think i want to achive is to find out if there is a free slot in the Schedule based on the given dtStart and dtEnde

  4. #4
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: SQL Statment help (Date)

    Sorry, I didn't realise you were using PostGre. I've only used it once or twice so my syntax knowledge isn't good but I think the problem is actually with your logic.

    You're checking that there's a record that starts after your new record ends and ends before you're new record starts. I imagine you're query returns a count of zero every time because you're checking for a logically impossible state. I think what you want is:-

    "SELECT Count(*) " & _
    "FROM [Table] " & _
    "WHERE ([datestart] <= " & makeDBDatumSQL(dtStart) & " " & _
    " AND [dateend] >= " & makeDBDatumSQL(dtStart) & ")" & _
    "OR ([dateend] >= " & makeDBDatumSQL(dtEnd) & " " & _
    " AND [datestart] =< " & makeDBDatumSQL(dtEnd) & ")" & _
    "OR ([datestart] >= " & makeDBDatumSQL(dtStart) & " " & _
    " AND [dateend] >= " & makeDBDatumSQL(dtEnd) & ")" & _
    "and lgppk = '" & strPK & "'"

    You're looking for rows that start before your new record starts but end after it starts (they overlap the beginning of your new record) or end after you new record ends but start before it ends (they overlap the end of your new redcord). Finally, you also want to grab any records that start after your new record starts and end before it finishes (they are wholly contained within the dates of your new record).

    I'm not sure but I think you could probably make this more efficient and readable by reversing all the boolean logic and checking for records which don't match them. I'm due to go home though so you'll have to experiment with that yourself.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    Resolved Re: SQL Statment help (Date)

    @FunkyDexter

    many thanks it help but if you should get out a more efficient solution i would be interested.

    In the meantime i close the thread as resolved and many thanks for the help.

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