Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to add percentage(%) symbol in stored procedure Expand / Collapse
Author
Message
Posted Wednesday, March 13, 2013 12:44 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, March 13, 2013 1:13 PM
Points: 18, Visits: 52
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??
Post #1430581
Posted Wednesday, March 13, 2013 2:09 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:58 PM
Points: 13,328, Visits: 12,826
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1430625
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse