简体   繁体   中英

Using special characters in SQL as string

Using SQL Server 2008

DECLARE @myVariable nvarchar (500)

SET @myVariable = 'select distinct b.*,v.vertrag_id,v.VersicherungsscheinNummer 
from CRM_Wifo_GmbH.dbo.vertrag_168 v,temp_universa b 
where v.VersicherungsscheinNummer like '%' + b.vsnr + '% 
and v.gesellschaft_id in('59','66')'

I have to set the value of this type in a variable. How could I do this? Is it possible? USING ' ' sign in a string?

You just need to escape the single quote ' using 2 single quotes instead ''

DECLARE @myVariable nvarchar (500)
SET @myVariable = 
N'select distinct b.*,v.vertrag_id,v.VersicherungsscheinNummer 
  from CRM_Wifo_GmbH.dbo.vertrag_168 v,temp_universa b 
  where v.VersicherungsscheinNummer like ''%'' + b.vsnr + ''% 
  and v.gesellschaft_id in(''59'',''66'')'

I am also using N' , so that I can span the string on multiple lines

Alternative solutions :

DECLARE @myVariable nvarchar (500)
SET @myVariable = 'select distinct b.*,v.vertrag_id,v.VersicherungsscheinNummer from CRM_Wifo_GmbH.dbo.vertrag_168 v,temp_universa b where v.VersicherungsscheinNummer like ' + char(39) + '%' + char(39) + ' + b.vsnr + ' + char(39) + '% and v.gesellschaft_id in(' + char(39) + '59' + char(39) + ',' + char(39) + '66' + char(39) + ')'

But i suggesst you, using 2 single quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM