0

Trying to use a single quote and assign it to a variable so I can create a dynamic report.

The problem is when I do this:

set @Statequery = (' and PostalState = ' ')  +  @PostalState  + '''

If you do this, print @Statequery, it needs it to look like this.

and postalState = 'QLD'

How do I escape the ' character in this case?

Thanks for your help in advanced,

Ryan

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Rya
  • 301
  • 2
  • 8
  • 13

2 Answers2

2

You need to escape it by doubling it:

set @Statequery = (' and PostalState = ''') + @PostalState + ''''
Oded
  • 477,625
  • 97
  • 867
  • 998
0

try this

DECLARE @PostalState  VARCHAR(50)='QLD'
DECLARE @Statequery VARCHAR(max)
set @Statequery = ' and PostalState = ''' +  @PostalState  +''''   
PRINT @Statequery 
sangram parmar
  • 8,024
  • 2
  • 21
  • 46