Sql server 如何解析T SQL中的特殊字符

Sql server 如何解析T SQL中的特殊字符,sql-server,function,tsql,sql-function,Sql Server,Function,Tsql,Sql Function,我正在编写一个SQL Server函数,它应该解析输入字符串并对其应用一些函数, 输入中的一些字符是“和”。如何在函数中识别它们 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) decl

我正在编写一个SQL Server函数,它应该解析输入字符串并对其应用一些函数, 输入中的一些字符是
。如何在函数中识别它们

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
Declare@Str varchar(300)
声明@CurrentChar--(“A”或“B”)和“C”
声明@positionint
设置@POSITION=0
声明@FLAG INT
设置@FLAG=0
声明@colName varchar(15)
声明@SearchKeyWord varchar(200)
设置@colName='article\u title'
设置@SearchKeyWord=''A'和'B''

而单引号(')的@POSITION转义字符本身就是单引号(')。 对于双引号(“),您可以按原样使用它

试试这个

DECLARE @sStr varchar(10)

SET @sStr = ''''

If @sStr = ('''')
    SELECT 'Str is '''
ELSE IF @sStr = ('"')
    SELECT 'Str is "'
ELSE 
    SELECT 'Other char'

我的问题是别的

我应该在进入While循环之前设置Str

set @Str = '';

:)

在Sqlserver端,您不会遇到任何问题,而不是使用所有特殊字符的单引号

只有单引号,只要你有单引号,你可以用双单引号代替。因此,sql将使用一个引号执行

Declare @Str varchar(300)
Declare @CurrentChar char    --("A" OR "B") AND "C"

DECLARE @POSITION INT = 0

DECLARE @FLAG INT = 0

declare @colName varchar(15) = 'article_title'
declare @SearchKeyWord varchar(200) = '"A" ''and "B"' --I added here the single quote(') and its works


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
CREATE TABLE SPECIAL_CH(S_CH VARCHAR(10))
插入到专用工具中

值(“”“”)、(“”“”)、(“”“”)、(“
如果@CurrentChar
(“”“”、“”“”)
@Mike它不起作用当我得到字符串的左字符时,它不可能是“”,它将是“”或“第一次使用替换(@sstr“”)”对于您得到的单个字符。然后应用上述条件。我更新代码,带*的行是未运行的代码行,有什么错误
--****suppose you want to check via sp, ****--
--alter procedure get_thedataWithParameterValueHaveSpecialChar
--(
declare @stringParameter Varchar(50) --suppose you passing the value from c# is : "a'b\"c"
print @stringParameter --will show at 
--)
--as
--begin
declare @find nvarchar(500), @replace nvarchar(500)
    Set @find =''+ char(39) +''  --this is the ascii value of single quote            
    Set @replace = ''+char(39)+char(39)+''

    Set @stringParameter =  '"'+ replace(@stringParameter,@find,@replace)  +'*"'                
print @stringParameter --will show the double single quote

--end   
CREATE TABLE SPECIAL_CH (S_CH VARCHAR(10))
INSERT INTO SPECIAL_CH
VALUES (''''),(';'),('"'),('<')

CREATE TABLE USER_TABLE (ID INT identity primary key,STRING VARCHAR(50))

INSERT INTO USER_TABLE
VALUES ('nothingin2'),('nothingin3'),('"charhere'),('charhere<'),('nothingin')

/*Multiple chars in string*/
INSERT INTO USER_TABLE
VALUES ('charshere<"')

/* USE [YourDB]
   GO SET ANSI_NULLS ON
   GO SET QUOTED_IDENTIFIER ON
   GO

ALTER FUNCTION dbo.FindS_CH (@input VARCHAR(255))
returns VARCHAR(255)
AS BEGIN

DECLARE @RETURN VARCHAR(255)
SET @RETURN = (SELECT  x.RESULT
                    FROM
                    (SELECT @input AS STRING) p
                    CROSS APPLY (select 
REPLACE(stuff((select ', ' + cast(S_CH as varchar(512))
        from SPECIAL_CH c 
        where p.STRING LIKE '%'+ c.S_CH +'%' 
        for xml path('')),1,2,''),'&lt','') as RESULT) x)

RETURN @RETURN
END
END*/




SELECT ID,STRING ,dbo.FindS_CH(STRING)
FROM USER_TABLE