Share via

Dynamic Pivot String throwing MSG 241 Level 156 State 1 error: Conversion failed when converting Date and/or time from character string

EagleFire26 41 Reputation points
2021-05-26T19:43:28.857+00:00

Hi Everyone I keep getting ,

MSG 241 Level 156 State 1 error: Conversion failed when converting Date and/or time
from character string when trying to print out my Dynamic SQL Query String to see
if it will print out correctly. I was curious I had quotes wrong or if my variables
for startdate and enddate which are datetime is incorrect and causing issues etc?

SET @SQLQUERY =
'
select * from
{
SELECT DISTINCT
QUOTENAME(CONCAT(YEAR(SHP.RECEIPT_DATE),''-'',FORMAT(MONTH(SHP.RECEIPT_DATE),''00''))) RECEIPT_MONTH
,QUOTENAME(CONCAT(YEAR(SHP.STATEMENT_START_DATE),''-'',FORMAT(MONTH(SHP.STATEMENT_START_DATE),''00''))) INCURRED_MONTH
,SHP.RBO_INST_ENCOUNTERS INST_ENCOUNTERS
FROM #TEMPSHP SHP
LEFT OUTER JOIN #TEMPHHR HHR ON SHP.CLAIM_HCC_ID = CLAIM_CLAIM_HCC_ID
WHERE RECEIPT_DATE BETWEEN ' + @STARTDATE + ' AND ' + @ENDDATE + ') AS BaseData
PIVOT
(SUM(INST_ENCOUNTERS)
FOR INCURRED_MONTH
IN (' + @PIVOTCOLUMN +') AS PIVOTTABLE

'

PRINT @SQLQUERY

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

0 comments No comments

Answer accepted by question author

EchoLiu-MSFT 14,626 Reputation points
2021-05-27T05:58:04.557+00:00

Hi @EagleFire26 ,

100132-image.png

I don’t know what your data and your query look like, so I can’t advise you. Please provide a minimal example that we can test. This includes the code you are trying now and the CREATE and INSERT statements, as well as the expected output of your example.

100093-image.png
Try:

 SET @SQLQUERY =  
 '  
 select * from  
 {  
 SELECT DISTINCT  
 QUOTENAME(CONCAT(YEAR(SHP.RECEIPT_DATE),''-'',FORMAT(MONTH(SHP.RECEIPT_DATE),''00''))) RECEIPT_MONTH  
 ,QUOTENAME(CONCAT(YEAR(SHP.STATEMENT_START_DATE),''-'',FORMAT(MONTH(SHP.STATEMENT_START_DATE),''00''))) INCURRED_MONTH  
 ,SHP.RBO_INST_ENCOUNTERS INST_ENCOUNTERS  
 FROM #TEMPSHP SHP  
 LEFT OUTER JOIN #TEMPHHR HHR ON SHP.CLAIM_HCC_ID = CLAIM_CLAIM_HCC_ID  
 WHERE RECEIPT_DATE BETWEEN ''' + cast(@STARTDATE as varchar) + ''' AND ''' +cast( @ENDDATEas varchar) + ''') AS BaseData  
 PIVOT  
 (SUM(INST_ENCOUNTERS)  
 FOR INCURRED_MONTH  
 IN (' + @PIVOTCOLUMN +') AS PIVOTTABLE  
      
 '  
      
 PRINT @SQLQUERY  

Regards
Echo


If the answer is helpful, please click "Accept Answer" and upvote it.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. EchoLiu-MSFT 14,626 Reputation points
    2021-05-27T03:11:17.207+00:00

    Or try:

    SET @SQLQUERY =  
    '  
    select * from  
    {  
    SELECT DISTINCT  
    QUOTENAME(CONCAT(YEAR(SHP.RECEIPT_DATE),''-'',FORMAT(MONTH(SHP.RECEIPT_DATE),''00''))) RECEIPT_MONTH  
    ,QUOTENAME(CONCAT(YEAR(SHP.STATEMENT_START_DATE),''-'',FORMAT(MONTH(SHP.STATEMENT_START_DATE),''00''))) INCURRED_MONTH  
    ,SHP.RBO_INST_ENCOUNTERS INST_ENCOUNTERS  
    FROM #TEMPSHP SHP  
    LEFT OUTER JOIN #TEMPHHR HHR ON SHP.CLAIM_HCC_ID = CLAIM_CLAIM_HCC_ID  
    WHERE RECEIPT_DATE BETWEEN ' + cast(@STARTDATE as date) + ' AND ' +cast( @ENDDATEas date) + ') AS BaseData  
    PIVOT  
    (SUM(INST_ENCOUNTERS)  
    FOR INCURRED_MONTH  
    IN (' + @PIVOTCOLUMN +') AS PIVOTTABLE  
      
    '  
      
    PRINT @SQLQUERY  
    

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


  2. Naomi Nosonovsky 8,906 Reputation points
    2021-05-26T19:49:07.183+00:00

    Change your query to be:

        SET @SQLQUERY =
        '
        select * from
        {
        SELECT DISTINCT
        QUOTENAME(CONCAT(YEAR(SHP.RECEIPT_DATE),''-'',FORMAT(MONTH(SHP.RECEIPT_DATE),''00''))) RECEIPT_MONTH
        ,QUOTENAME(CONCAT(YEAR(SHP.STATEMENT_START_DATE),''-'',FORMAT(MONTH(SHP.STATEMENT_START_DATE),''00''))) INCURRED_MONTH
        ,SHP.RBO_INST_ENCOUNTERS INST_ENCOUNTERS
        FROM #TEMPSHP SHP
        LEFT OUTER JOIN #TEMPHHR HHR ON SHP.CLAIM_HCC_ID = CLAIM_CLAIM_HCC_ID
        WHERE RECEIPT_DATE BETWEEN @STARTDATE AND @ENDDATE ) AS BaseData
        PIVOT
        (SUM(INST_ENCOUNTERS)
        FOR INCURRED_MONTH
        IN (' + @PIVOTCOLUMN +') AS PIVOTTABLE ';
    
        PRINT @SQLQUERY;
    
        EXECUTE sp_ExecuteSQL @SQLQuery, N'@StartDate DATE, @EndDate DATE', @StartDate = @StartDate, @EndDate = @EndDate;
    

    Don't embed the parameters inside the query - it is a very bad practice which will lead to errors like you're getting.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.