I'm writing an SQL Server function which should parse the input string and apply some function on it,
some characters in the input are "
and '
. How can I recognize them in function?
Declare @Str varchar(300)
Declare @CurrentChar char --("A" OR "B") AND "C"
DECLARE @POSITION INT
SET @POSITION = 0
DECLARE @FLAG INT
SET @FLAG= 0
declare @colName varchar(15)
declare @SearchKeyWord varchar(200)
set @colName='article_title'
set @SearchKeyWord='"A" and "B"'
WHILE @POSITION <LEN(@SearchKeyWord)
BEGIN
SET @CurrentChar = SUBSTRING(@SearchKeyWord,@POSITION+1,@POSITION+2)
print 'CurrentChar ' + @CurrentChar
* if @CurrentChar = ('"')
BEGIN
IF (@FLAG=0)
BEGIN
SET @FLAG=1;
SET @Str = @Str + @colName + ' LIKE ''%';
END
ELSE --FLAG=1 : end of the parsing word
BEGIN
SET @FLAG = 0 ;
SET @Str = @Str + '%'' ';
END
print 'str: ' + @Str
END
if (@CurrentChar = (' ') OR @CurrentChar = ('(') OR @CurrentChar = (')') OR (ASCII(@CurrentChar) BETWEEN 65 and 90) OR (ASCII(@CurrentChar) BETWEEN 97 and 122) OR (ASCII(@CurrentChar) BETWEEN 48 and 57))
BEGIN
--print 'else'
SET @Str = @Str + @CurrentChar ;
print 'str: ' + @Str
END
SET @POSITION = @POSITION + 1
END
Rather than single quote in all special characters, at Sqlserver side, no issue you will be face.
The only thing with single quote, whenever you have single quote, you can replace with double single quote. So sql will execute with a single quote.
Here I give some explanation while working with C# code and Sql procedure .