0

我已经阅读了很多文章,包括在 stackoverflow 上的文章,为了找到子字符串,应该使用以下内容:

IF CHARINDEX('mywoRD', @words) > 0
BEGIN
    -- DO SOMETHING
END

我正在尝试以下但它不工作它总是返回错误:

假设我有一个字符串'Basketball & Soccer',我正在尝试编写一个脚本来检查与符号并将其编码为&所以它变成了'Basketball & Soccer'我的问题是数据库中可能还有另一个这样的'Basketball & Soccer'

所以当我运行我的脚本时,第二个它变成:

'Basketball & Soccer'

我正在尝试以下方法来清理它:

DECLARE @cleanparam as varchar(500)   

IF CHARINDEX('&',@myparameter) > 0 
BEGIN 
   -- if & is in the string skip and start with quotes
   SELECT @cleanparam = REPLACE(@myparameter,'"','"')   
END
ELSE
 BEGIN
      -- if not clean &'s and quotes and then continue with others
      SELECT @cleanparam = REPLACE(@myparameter,'&','&')   
      SELECT @cleanparam = REPLACE(@cleanparam,'"','"')   
 END

      SELECT @cleanparam = REPLACE(@cleanparam, '©', '©')
      SELECT @cleanparam = REPLACE(@cleanparam, '«', '«') 
       -- OTHERS HERE
4

2 回答 2

1

如果没有办法保护脚本不被多次调用,那么您需要对其进行增强,首先将编码的序列转换回值,然后再转换回序列。像这样:

  -- Convert back to symbols
  SELECT @cleanparam = REPLACE(@myparameter,'&','&')   
  SELECT @cleanparam = REPLACE(@cleanparam,'"','"')   
  etc...

  -- Convert symbols to escape sequences
  SELECT @cleanparam = REPLACE(@cleanparam,'&','&')   
  SELECT @cleanparam = REPLACE(@cleanparam,'"','"')   
  etc...
于 2014-02-06T03:56:29.773 回答
1

尝试这个

DECLARE @myparameter as varchar(500) 
set @myparameter = 'Basketball & Soccer & Volleyball'


DECLARE @cleanparam as varchar(500)
SELECT @cleanparam = REPLACE(@myparameter,'&amp;','&') --<----put some trick here :)
SELECT @cleanparam = REPLACE(@cleanparam,'&','&amp;')   
SELECT @cleanparam = REPLACE(@cleanparam,'"','&quot;') 
SELECT @cleanparam = REPLACE(@cleanparam, '©', '&copy;')
SELECT @cleanparam = REPLACE(@cleanparam, '«', '&laquo;') 

SELECT @cleanparam
于 2014-02-06T03:57:23.697 回答