Results 1 to 15 of 15

Thread: [RESOLVED] BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved [RESOLVED] BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    Hello

    SQL Server 2005

    I have tried and cannot return any rows either though there are records that should be returned, using the sql below.

    Code:
    SELECT * FROM Calls WHERE clientName IN ('Bankside Engineering Limited') 
    AND scheduleDate BETWEEN 20/5/2007 AND 30/5/2007
    In the database scheduleDate is a dateTime datatype.

    In the database I have copied and pasted.
    23/05/2007 00:00:00

    I tried the following
    Code:
    SELECT * FROM Calls WHERE clientName IN ('Bankside Engineering Limited') 
    AND scheduleDate BETWEEN '20/5/2007' AND '30/5/2007'
    And got an error message:
    The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

    Many thanks for any help with this,

    Steve
    steve

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    Sql server uses american dates, ie mm/dd/yyyy.

    Szlamany can provide further info - or there is the faq at the top or search online/help files.

    You may prefer to use dd-mmm-yyyy format as at least you definately know the month is right.


    Also, in oracle it requests that dates provided are dates and not strings, so you may need to convert to a date (not sure of the sql server function, but oracle is to_date('string','format'))

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    The dates need to be converted to US standard date format (mm/dd/yyyy) when performing SQL seraches with them

    So I think the query would look like this:

    Code:
    Select * From Calls Where ClienName In ('Bankside Engineering Limited') 
    AND scheduleDate BETWEEN '5/20/2007' AND '5/30/2007'
    I'm assuming that 20/5 is May 20th and 30/5 is May 30th
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    Quote Originally Posted by steve_rm
    Code:
    SELECT * FROM Calls WHERE clientName IN ('Bankside Engineering Limited') 
    AND scheduleDate BETWEEN '20/5/2007' AND '30/5/2007'
    Try to avoid using the date format you have used. I have explained why this is bad in this thread http://vbforums.com/showpost.php?p=2895295&postcount=16

    Also, there is no need to use IN when you only have one value. Use = instead.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    Hello,

    I am still having a problem, i tried the following
    vb Code:
    1. SELECT * FROM Calls WHERE clientName IN ('Bankside Engineering Limited')
    2. AND [Date] BETWEEN '20070521' AND '20070531'

    I have this date stored in the database, but does not return and it should.
    23/05/2007 00:00:00

    using the format yyyymmdd


    Thanks,
    Last edited by steve_rm; Jun 1st, 2007 at 08:29 AM. Reason: Made error when typing
    steve

  6. #6
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    Wrap the dates in single quotes like this '20070530'

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    Hello,

    Thanks for your continued help.

    I have done this now. but still no rows returned.
    vb Code:
    1. SELECT * FROM Calls
    2. WHERE [Date] BETWEEN '20070521' AND '20070531'

    This date is stored like this in the database, it should be returned.
    23/05/2007 00:00:00
    steve

  8. #8
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    What is the format of the date stored in the database? (dd/mm/yyyy or yyyymmdd). If you want to use the yyyymmdd and the stored data is dd/mm/yyyy then you need to format the database data in the in Where condition to the same. Or use US date format.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  9. #9
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    Quote Originally Posted by GaryMazzone
    What is the format of the date stored in the database? (dd/mm/yyyy or yyyymmdd). If you want to use the yyyymmdd and the stored data is dd/mm/yyyy then you need to format the database data in the in Where condition to the same. Or use US date format.
    Sorry, that is wrong. A date is always stored at two 4 byte integers. The first bytes representing number of days after 01-01-1900 and the second four bytes as number of 1/300 seconds after midnight.

    When you SELECT dates from the table they are formatted based on the regional settings on your pc.

    When you hardcode datetime columns in a WHERE clause, as done by steve_rm, SQL Server will try to convert the text string into a valid date format. The problem is that there are many different formats using / as the date separator. If you write 05/06/07, which for you is May 6 2007 (US format), for me that is June 5 2007. There are two more formats using /.
    The safest thing is therefore to write the date string without any separators. Why? Because the only format not using any separators is the ISO format, and it is ALWAYS yyyymmdd.

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    It might be the safest to save as yyyymmdd but if he already has the dates as dd/mm/yyyy then he needs to do something else just passing in yyyymmdd will not help him as he has already stated. Just try what I suggested and it will probably work. The ANSI standard for SQL uses US date format for dates.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  11. #11
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    Quote Originally Posted by GaryMazzone
    It might be the safest to save as yyyymmdd but if he already has the dates as dd/mm/yyyy then he needs to do something else just passing in yyyymmdd will not help him as he has already stated.
    As I said, using yyyymmdd will always work. Trust me

    steve_rm, can you paste rthe results of the following select here:

    Code:
    select top 20 from convert(varchar(8), [Date],112) Calls where clientname='Bankside Engineering Limited'

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    Hello,

    Still having a problem, sorry. The code I am using is this.
    vb Code:
    1. SELECT * FROM Calls
    2. WHERE [Date] BETWEEN '20070521' AND '20070531'

    In my database i have a date like this: 23/05/2007 00:00:00

    Now my query should return this row, but it doesn't.

    I changed my regional setting on my computer to US standard. That didn't make any difference.

    Any other ideas?
    steve

  13. #13
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    Did you try the query I suggested in Post#3?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  14. #14
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    How are you executing this query? If you are using VB, ADO and populating a recordset, how do you check if there are no records? You may want to post that code.

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: BETWEEN 20/5/2007 AND 30/5/2007 no rows returned

    Thanks guys, finally got this sorted.

    It was a mixture of the above examples.

    Thanks
    steve

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