The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

  • Hi all, hope in your help.

    I have problem to execute query with interval date.

    If try this query I don't have problem and the output is correct:

    SELECT * FROM dotable

    WHERE

    dotableDate BETWEEN CONVERT (datetime, '01/01/2015', 121)

    AND CONVERT (datetime, '09/01/2015', 121);

    Instead if try this I have error:

    SELECT * FROM dotable

    WHERE

    dotableDate BETWEEN CONVERT (datetime, '25/01/2015', 121)

    AND CONVERT (datetime, '28/01/2015', 121);

    [Err] 22007 - [SQL Server]The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

    Why?

    Can you help me?

    Thank you in advance.

  • cms9651 (5/14/2015)


    Hi all, hope in your help.

    I have problem to execute query with interval date.

    If try this query I don't have problem and the output is correct:

    SELECT * FROM dotable

    WHERE

    dotableDate BETWEEN CONVERT (datetime, '01/01/2015', 121)

    AND CONVERT (datetime, '09/01/2015', 121);

    Instead if try this I have error:

    SELECT * FROM dotable

    WHERE

    dotableDate BETWEEN CONVERT (datetime, '25/01/2015', 121)

    AND CONVERT (datetime, '28/01/2015', 121);

    [Err] 22007 - [SQL Server]The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

    Why?

    Can you help me?

    Thank you in advance.

    You're using the wrong STYLE parameter for the conversion you require. 121 corresponds to [yyyy-mm-dd hh:mi:ss.mmm(24h)].Check the CONVERT section here, and try 103 instead.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (5/14/2015)


    cms9651 (5/14/2015)


    Hi all, hope in your help.

    I have problem to execute query with interval date.

    If try this query I don't have problem and the output is correct:

    SELECT * FROM dotable

    WHERE

    dotableDate BETWEEN CONVERT (datetime, '01/01/2015', 121)

    AND CONVERT (datetime, '09/01/2015', 121);

    Instead if try this I have error:

    SELECT * FROM dotable

    WHERE

    dotableDate BETWEEN CONVERT (datetime, '25/01/2015', 121)

    AND CONVERT (datetime, '28/01/2015', 121);

    [Err] 22007 - [SQL Server]The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

    Why?

    Can you help me?

    Thank you in advance.

    You're using the wrong STYLE parameter for the conversion you require. 121 corresponds to [yyyy-mm-dd hh:mi:ss.mmm(24h)].Check the CONVERT section here, and try 103 instead.

    thank you very much!

  • Or much better, when entering literal dates and making date/datetime comparisons:

    1) always use format YYYYMMDD, which is 100% under any/all SQL settings

    2) use >= and < [the next day] rather than between

    WHERE

    dotableDate >= '20150101' AND

    dotableDate < '20150110'

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply