aman6496
aman6496

Reputation: 153

Single quotes in variable in T-SQL

I have a condition where user will pass comma separated values like

0071061386,0071061387

it will be passed to a variable which will feed the values into a dynamic query with two single quotes.

passed by user

declare @s nvarchar(max) = '0071061386,0071061387'

it should be like after converting the user values so that I can pass the values into dynamic query

declare @s nvarchar(max) = '''0071061386'',''0071061387'''

Upvotes: 0

Views: 1578

Answers (2)

Thangadurai.B
Thangadurai.B

Reputation: 561

Try this

  DECLARE @S NVARCHAR(MAX) = '0071061386,0071061387'
  SELECT REPLACE(''''''''+@S+'''''''',',',''''',''''')

Upvotes: 0

Zohar Peled
Zohar Peled

Reputation: 82474

A simple replace with probably do the trick here, but I beleive this to be an XYProblem. Perhaps you better explain the problem leading you to go this path in the first place.

declare @s nvarchar(max) = '0071061386,0071061387'

SELECT '''''' + REPLACE(@s, ',', ''''',''''') + '''''' 

Result:

''0071061386'',''0071061387''

Upvotes: 2

Related Questions