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
  • 732,580
  • 175
  • 1,330
  • 1,459
Rya
  • 321
  • 2
  • 8
  • 13

2 Answers2

2

You need to escape it by doubling it:

set @Statequery = (' and PostalState = ''') + @PostalState + ''''
Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

try this

DECLARE @PostalState  VARCHAR(50)='QLD'
DECLARE @Statequery VARCHAR(max)
set @Statequery = ' and PostalState = ''' +  @PostalState  +''''   
PRINT @Statequery 
sangram parmar
  • 8,462
  • 2
  • 23
  • 47