How to add percentage(%) symbol in stored procedure

  • Hi All,

    I want to know how to add percentage symbol(%) to my final sql statment column names in stored procedure, query below

    set @finalSelect = 'SELECT NAME,State,TotalPop, ' + @parameter +'_Pop%

    FROM

    '

    Result: NAME, State, TotalPop, White_Pop% (column names)

    When I add % symbol it throws an error:

    Msg 102, Level 15, State 1, Line 1

    Incorrect syntax near '%'.

    Any idea how to add percentage(%) symbol??

  • sql4us (3/13/2013)


    Hi All,

    I want to know how to add percentage symbol(%) to my final sql statment column names in stored procedure, query below

    set @finalSelect = 'SELECT NAME,State,TotalPop, ' + @parameter +'_Pop%

    FROM

    '

    Result: NAME, State, TotalPop, White_Pop% (column names)

    When I add % symbol it throws an error:

    Msg 102, Level 15, State 1, Line 1

    Incorrect syntax near '%'.

    Any idea how to add percentage(%) symbol??

    Personally I would not use symbols like that in column names. That type of thing should done in the front end.

    The direct answer to your question however is you have to wrap the column name inside square brackets.

    set @finalSelect = 'SELECT NAME,State,TotalPop, [' + @parameter +'_Pop%]'

    Be careful here, it looks like you are receiving a parameter and then directly executing the dynamic sql you are creating. This is vulnerable to sql injection.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply