The conversion of a char data type to a datetime data type resulted in an out ofrange

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • syedshaffee
    New Member
    • Jan 2012
    • 91

    The conversion of a char data type to a datetime data type resulted in an out ofrange

    hey people,

    when i am trying to execute a query i am getting
    'The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.'ERROR
    when i am generating for a short period EG:- form 1st jan to 15 jan for the same year.

    here is the query can u people suggest me something
    Code:
    SELECT DISTINCT ADInvoice.InvoiceNo, ADInvoice.InvoiceDate, ADInvoice.InvoiceID AS InvoiceID, ADRVAllocation.ContractNo AS ContractNo, Sum(ADAsset.AssetPrice) AS SumOfAssetPrice, ADRVAllocation.ADRVAllocation, ADInvoice.InvoiceTotal, ADInvoice.InvoiceTypeID, ADInvoice.InvoiceGeneratedDate 
    FROM (ADInvoice LEFT JOIN ADRVAllocation ON ADInvoice.InvoiceID = ADRVAllocation.InvoiceID) LEFT JOIN ADAsset ON (ADRVAllocation.InvoiceID = ADAsset.AssetInvoiceID) AND (ADRVAllocation.ContractNo = ADAsset.AssetContractNo) 
    GROUP BY ADInvoice.InvoiceNo, ADInvoice.InvoiceDate, ADInvoice.InvoiceID, ADRVAllocation.ContractNo, ADRVAllocation.ADRVAllocation, ADInvoice.InvoiceTotal, ADInvoice.InvoiceTypeID, ADInvoice.InvoiceGeneratedDate 
    HAVING ((ADInvoice.InvoiceTypeID=2) AND (ADInvoice.InvoiceGeneratedDate BETWEEN convert(DATETIME,'31-01-2012',110) AND CONVERT(DATETIME,GETDATE(),110)))
    Union 
    SELECT DISTINCT ADInvoice.InvoiceNo, ADInvoice.InvoiceDate, ADAsset.AssetDamagesInvoiceID AS InvoiceID, ADRVAllocation.ContractNo AS ContractNo, Sum(ADAsset.AssetInvoiceCost) AS SumOfAssetPrice, ADRVAllocation.ADRVAllocation, ADInvoice.InvoiceTotal, ADInvoice.InvoiceTypeID, ADInvoice.InvoiceGeneratedDate 
    FROM (ADInvoice LEFT JOIN ADRVAllocation ON ADInvoice.InvoiceID = ADRVAllocation.InvoiceID) LEFT JOIN ADAsset ON (ADRVAllocation.InvoiceID = ADAsset.AssetInvoiceID) AND (ADRVAllocation.ContractNo = ADAsset.AssetContractNo) 
    GROUP BY ADInvoice.InvoiceNo, ADInvoice.InvoiceDate, ADAsset.AssetDamagesInvoiceID, ADRVAllocation.ContractNo, ADRVAllocation.ADRVAllocation, ADInvoice.InvoiceTotal, ADInvoice.InvoiceTypeID, ADInvoice.InvoiceGeneratedDate 
    HAVING ADAsset.AssetDamagesInvoiceID Is Not Null AND ADInvoice.InvoiceTypeID=2 AND ADInvoice.InvoiceGeneratedDate >= convert(DATETIME,'01/01/2012',110) AND ADInvoice.InvoiceGeneratedDate <= CONVERT(DATETIME,GETDATE(),110)
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You should look at the actual date values and make sure they're within range. If you don't see which one is causing the error, post some of the date values.

    Comment

    • syedshaffee
      New Member
      • Jan 2012
      • 91

      #3
      Code:
      ---+----------------------+---+----------------------+
      ID | Intent Date          |ADI| Invoice_date         |
      ---+----------------------+---+----------------------+
      1  |01/01/2005 12:00:00 AM|43 |01/01/2005 12:00:00 AM|
      ---+----------------------+---+----------------------+
      This are some data values form My table

      can u Suggest me How do i resolve the Error

      Comment

      • Stewart Ross
        Recognized Expert Moderator Specialist
        • Feb 2008
        • 2545

        #4
        Three observations. The first is that you are specifying US format (mm/dd/yyyy) in your convert statement, yet you are supplying a UK or other non-US format date literal in line 4 of the code above. Second observation relating to the same date literal is that it is supplied using dash separators rather than slash separators - I don't know if that is significant or not but it is inconsistent at best.

        The statement concerned is:

        Code:
        convert(DATETIME,'31-01-2012',110)
        which should be

        Code:
        convert(DATETIME,'01/31/2012',110)
        The third observation is that you are using Convert on the system GetDate function which seems a bit redundant to me, as GetDate already returns a value of type datetime. For example, in line 9 above:

        Code:
        CONVERT(DATETIME,GETDATE(),110)
        -Stewart

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32656

          #5
          I've just added a post to Literal DateTimes and Their Delimiters (#) explaining where the #yyyy-mm-dd# date format fits into the ANSI SQL standards. It's sort of supported in Jet (Access SQL), but fully supported in SQL Server. The new link in the article gives an MSDN page with full details for SQL Server.

          The short version is that it's a perfectly valid and standard format for SQL date literals though.

          Comment

          • syedshaffee
            New Member
            • Jan 2012
            • 91

            #6
            Okay people now i checked it working fine in sql server 2005 but when I'm executing on my front end it gives me an ODBC ERROR:3151 can u suggest me some thing

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32656

              #7
              From an error number on its own? I doubt it.

              You need to post the error message, your code and something to indicate whereabout in your code the error occurred. I would have thought that would be obvious.

              PS. OP has posted this question separately under How to reslove ODBC 3151: Error, so please address any answers to this question there and not here. This question can be considered to be closed.
              Last edited by NeoPa; Feb 6 '12, 10:22 AM. Reason: Added PS

              Comment

              Working...