ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Format Date column in dynamic pivot query in SQL Server<p>You need to pass single quote value in string as expected for @DynamicPivoteQuery value if it&rsquo;s not pass correctly it will not execute the @DynamicPivoteQuery as there are syntax error problem.</p> <p>Refer the below test query and impliment it as per your code logic.</p> <p><span style="text-decoration: underline;"><strong>SQL</strong></span></p> <pre class="brush: sql">DECLARE @DynamicPivotQuery AS NVARCHAR(MAX) DECLARE @ColumnName AS NVARCHAR(MAX) DECLARE @StartDate DATETIME SELECT @ColumnName= ISNULL(@ColumnName + &#39;,&#39;,&#39;&#39;) + QUOTENAME(CatName) FROM (SELECT DISTINCT CatName FROM tblCategory) AS BCats SET @DynamicPivotQuery = &#39;SELECT REPLACE(CONVERT(CHAR(15), RcvdDate, 106),&#39;&#39; &#39;&#39;,&#39;&#39; - &#39;&#39;) AS RcvdDate, &#39; + @ColumnName + &#39; from ( SELECT RcvdDate, CatName, Amount FROM tblTransaction INNER JOIN tblCategory ON #tblCategory.CatNo = tblTransaction.BCatId ) x PIVOT ( SUM(x.Amount) for x.CatName in (&#39; + @ColumnName + &#39;) ) P&#39; EXEC(@DynamicPivotQuery);</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/947130/Format-Date-column-in-dynamic-pivot-query-in-SQL-Server/https://www.aspforums.net:443/Threads/947130/Format-Date-column-in-dynamic-pivot-query-in-SQL-Server/Mon, 13 Nov 2017 03:37:28 GMT