简体   繁体   中英

Escape characters SQL Server 2008 R2 [%]

I am trying to pivot a resultset, one of the values in the rows which iam comparing against is COutputData_2_Magnesium Stearate [%]. SQL does not seem to like the [%] symbol, i dont have anyway to change the how the name is shown in the result set. Is there a way I can escape [%] when pivoting. Thanks.

You should use the QUOTENAME function to get the proper escaping for possible non standard identifiers.

SELECT QUOTENAME('COutputData_2_Magnesium Stearate [%]')

returns

[COutputData_2_Magnesium Stearate [%]]]

So use that.

Example

WITH T(Id, Val)
     AS (SELECT 1, 'COutputData_2_Magnesium Stearate [%]')
SELECT *
FROM   T PIVOT ( AVG(Id) FOR Val IN ([COutputData_2_Magnesium Stearate [%]]]) ) AS P;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM