troubleshooting Question

Syntax error converting character string to smalldatetime data type.

Avatar of krisred
krisred asked on
Microsoft SQL Server
6 Comments1 Solution469 ViewsLast Modified:
I have the following stored procedure, when I run I am getting an error:
Syntax error converting character string to smalldatetime data type.

I can't seem to figure out what the problem is??

---BEGIN PROC ---

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO


ALTER PROCEDURE dbo.spGetPURSXmlExport
(
      @dtmStartDate SMALLDATETIME,
      @dtmEndDate SMALLDATETIME
)
AS
SET NOCOUNT ON
DECLARE @strSQL                                    VARCHAR(8000),
      @strXML                                    VARCHAR(8000),
      @strTmpProducts                         VARCHAR(150),
      @strSiteCategory                        VARCHAR(255),
      @strLSiteName                              VARCHAR(255),
      @strWaterBasin                              VARCHAR(255),
      @strZipCode                              VARCHAR(10),
      @strdtmDate                              VARCHAR(10),
      @strLChemicalProductName                  VARCHAR(500),
      @strLEPARegNumber                        VARCHAR(100),
      @strLSLN                              VARCHAR(100),
      @strLSection18                              VARCHAR(100),
      @strLPurpose                              VARCHAR(255),
      @strdblAmount                              VARCHAR(255),
      @strUnit                              VARCHAR(100),
      @strRandomString                        VARCHAR(5),
      @ptrval                               BINARY(16)

CREATE TABLE #tblUseReport
(
      strSiteCategory                  VARCHAR(255) NOT NULL,
      strSiteName                  VARCHAR(255)        NULL,
      strWaterBasin                  VARCHAR(255)        NULL,
      strZipCode                  VARCHAR(10)        NULL,
      strdtmDate                  VARCHAR(50)       NULL,
      strChemicalProductName            VARCHAR(500)        NULL,
      strEPARegNumber                    VARCHAR(100)        NULL,
      strSLN                        VARCHAR(100)        NULL,
      strSection18                  VARCHAR(100)        NULL,
      strPurpose                  VARCHAR(255)        NULL,
      strdblAmount                  VARCHAR(255)        NULL,
      strUnit                        VARCHAR(100)        NULL
)

CREATE TABLE #tblXmlUseReport
(
      XmlUseReport             TEXT

)

SET @strSQL = ' INSERT INTO #tblUseReport(  ' +
      'strSiteCategory,  ' +
      'strSiteName,  ' +
      'strWaterBasin,  ' +
      'strdtmDate,  ' +
      'strChemicalProductName,  ' +
      'strEPARegNumber,  ' +
      'strPurpose,  ' +
      'strdblAmount,  ' +
      'strUnit)  ' +
'SELECT dbo.fnEncodeXml(''Agriculture''),  ' +
      'dbo.fnEncideXml(''Nursery/Christmas Trees''),  ' +
      'dbo.fnEncodeXml(''Willamette''),  ' +
      'CONVERT(VARCHAR,dtmDateApplied,101)  ' +
      'dbo.fnEncodeXml(SUB.strChemicalProductAltName),  ' +
      'dbo.fnEncodeXml(SUB.strEPARegNumber),  ' +
      'dbo.fnEncodeXml(COALESCE(SUB1.strTargetTypeAltName,''None'')),  ' +
      'COALESCE(b.dblTotalAmount,''0''),  ' +
      'COALESCE(d.strUnitAltName,''None'') ' +
'FROM dbo.tblSAWorkOrder a  ' +
      'INNER JOIN dbo.tblSAWorkOrderChemicals b  ' +
            'ON a.strWorkOrderNumber = b.strWorkOrderNumber  ' +
      'LEFT JOIN  ' +
      '(SELECT  ' +
            'c.intChemicalProductID,  ' +
            'c.strChemicalProductAltName,  ' +
            'c.strEPARegNumber,  ' +
            'c.ysnNonReportable  ' +
      'FROM  ' +
            'dbo.tblChemicalProducts c  ' +
      ') AS SUB ON b.intChemicalID = SUB.intChemicalProductID  ' +
      'LEFT JOIN  ' +
      'dbo.tblUnits d  ' +
      'ON b.intRateSUnitID = d.intUnitID  ' +
      'LEFT JOIN  ' +
      '(SELECT  ' +
            'e.strWorkOrderNumber,  ' +
            'MAX(g.strTargetTypeAltName) AS strTargetTypeAltName  ' +
      'FROM  ' +
            'dbo.tblSAWorkOrderPlantTarget e  ' +
            'INNER JOIN  ' +
            'dbo.tblTargetTypeTarget f  ' +
            'ON e.intTargetID = f.intTargetID  ' +
            'LEFT JOIN  ' +
            'dbo.tblTargetType g ON  ' +
            'f.intTargetTypeID = g.intTargetTypeID  ' +
      'GROUP BY  ' +
            'e.strWorkOrderNumber) AS SUB1 ON a.strWorkOrderNumber = SUB1.strWorkOrderNumber  ' +
'WHERE a.intWorkOrderStage = ''7'' AND a.ysnScoutOnly <> ''1'' AND SUB.ysnNonReportable <> ''1''  ' +
      'AND a.dtmDateApplied BETWEEN ' + @dtmStartDate + ' AND ' + @dtmEndDate +
'GROUP BY a.dtmDateApplied,  ' +
      'SUB.strEPARegNumber,  ' +
      'SUB.strChemicalProductAltName,  ' +
      'COALESCE(SUB1.strTargetTypeAltName,''None''),  ' +
      'COALESCE(d.strUnitAltName,''None'')  ' +
'ORDER BY a.dtmDateApplied  '
      exec (@strSQL)

      SET @strXML = '<?xml version="1.0" encoding="UTF-8"?>'
      SET @strXML = @strXML + '<!DOCTYPE Submission SYSTEM "http://purs-reports.oda.state.or.us/WebObjects/PURSReporter.woa/Contents/WebServerResources/purs.dtd">'
      SET @strXML = @strXML + '<Submission version="1.0">'

      INSERT INTO #tblXmlUseReport VALUES(@strXML)
      SET @strXML = ''

      DECLARE UserReport CURSOR FOR
      SELECT       strSiteCategory, strSiteName, strWaterBasin, strZipCode, strdtmDate
      FROM #tblUseReport
      GROUP BY strSiteCategory, strSiteName, strWaterBasin, strZipCode, strdtmDate

      OPEN UserReport;
      FETCH NEXT FROM UserReport INTO
      @strSiteCategory, @strLSiteName, @strWaterBasin, @strZipCode, @strdtmDate
--      @strSiteCategory, @strLSiteName, @strWaterBasin, @strZipCode, @strdtmDate, @strLChemicalProductName, @strLEPARegNumber, @strLSLN, @strLSection18, @strLPurpose, @strdblAmount, @strUnit
      
      WHILE @@FETCH_STATUS = 0
      BEGIN
            EXEC spGenerateRandomString @strRandomString OUTPUT

            SET @strXML = '<UseReport id="' + @strRandomString + '">'
            SET @strXML = @strXML + '<SiteCategory>' + LTRIM(RTRIM(@strSiteCategory)) + '</SiteCategory>'
            SET @strXML = @strXML + COALESCE('<SpecificSite>' + @strLSiteName + '</SpecificSite>', '')
            SET @strXML = @strXML + COALESCE('<useDate>' + @strdtmDate + '</useDate>', '')
            IF ISNULL(@strZipCode, '') <> '' OR ISNULL(@strWaterBasin, '') <> ''
            BEGIN
                  SET @strXML = @strXML + '<UseLocation>'                  
                  SET @strXML = @strXML + COALESCE('<UseLocationWaterBasin><waterBasin>' + @strWaterBasin + '</waterBasin></UseLocationWaterBasin>', '')
                  SET @strXML = @strXML + COALESCE('<UseLocationZipCode><zipCode>' + @strZipCode + '</zipCode></UseLocationZipCode>', '')
                  SET @strXML = @strXML + '</UseLocation>'
            END

            SELECT @ptrval = TEXTPTR(XmlUseReport)
            FROM #tblXmlUseReport
            UPDATETEXT #tblXmlUseReport.XmlUseReport @ptrval NULL 0 @strXML
            SET @strXML = ''

            SELECT @strXML = @strXML +
            '<UseReportProduct>' +
                  COALESCE('<epaProductNumber>' + CONVERT(VARCHAR, strEPARegNumber) + '</epaProductNumber>', '') +                  
                  COALESCE('<otherProductNumber>' + CASE
                                                WHEN strEPARegNumber IS NULL AND strSLN IS NOT NULL THEN strSLN
                                                WHEN strEPARegNumber IS NULL AND strSection18 IS NOT NULL THEN strSection18
                                                ELSE NULL
                                            END
                        + '</otherProductNumber>', '') +                  
                  COALESCE('<productName>' + strChemicalProductName + '</productName>', '') +
                  COALESCE('<purpose>' + strPurpose + '</purpose>', '') +
                  COALESCE('<quantity>' + strdblAmount + '</quantity>', '') +
                  COALESCE('<quantityUnit>' + strUnit + '</quantityUnit>', '') +
            '</UseReportProduct>'
            FROM #tblUseReport
            WHERE             strSiteCategory = @strSiteCategory
                  AND        COALESCE(strSiteName, '') = COALESCE(@strLSiteName, '')
                  AND       COALESCE(strWaterBasin, '') = COALESCE(@strWaterBasin, '')
                  AND       COALESCE(strZipCode, '') = COALESCE(@strZipCode, '')
                  AND       COALESCE(strdtmDate, '') = COALESCE(@strdtmDate, '')
                  
            SET @strXML = '<products>' + @strXML + '</products></UseReport>'

            SELECT @ptrval = TEXTPTR(XmlUseReport)
            FROM #tblXmlUseReport
            UPDATETEXT #tblXmlUseReport.XmlUseReport @ptrval NULL 0 @strXML
            SET @strXML = ''

            SET @strTmpProducts = ''

            FETCH NEXT FROM UserReport INTO
            @strSiteCategory, @strLSiteName, @strWaterBasin, @strZipCode, @strdtmDate
      END
      CLOSE UserReport
      DEALLOCATE UserReport

      SET @strXML = '</Submission>'
      SELECT @ptrval = TEXTPTR(XmlUseReport)
      FROM #tblXmlUseReport
      UPDATETEXT #tblXmlUseReport.XmlUseReport @ptrval NULL 0 @strXML
      SET @strXML = ''
      SELECT XmlUseReport FROM #tblXmlUseReport

      DROP TABLE #tblXmlUseReport
      DROP TABLE #tblUseReport
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

---END PROC--

ASKER CERTIFIED SOLUTION
Daniel Reynolds
Software Applications Developer / Integrator

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 6 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 6 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004