您可以执行以下操作:
SELECT * FROM PART_TABLE
WHERE REPLACE(REPLACE(PARTNO,'/', ''),'-','') LIKE '%D12A%'
这适用于您指定的 2 个字符,并且可以扩展为更多字符,如下所示:
SELECT * FROM PART_TABLE
WHERE REPLACE(REPLACE(REPLACE(PARTNO,'/', ''),'-',''),*,'') LIKE '%D12A%'
除非您的特殊字符有限,否则可能不是最优雅的解决方案。否则,我建议编写一个函数来去除非字母数字字符。
以下是此类函数的示例:
CREATE FUNCTION dbo.udf_AlphaNumericChars
(
@String VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @RemovingCharIndex INT
SET @RemovingCharIndex = PATINDEX('%[^0-9A-Za-z]%',@String)
WHILE @RemovingCharIndex > 0
BEGIN
SET @String = STUFF(@String,@RemovingCharIndex,1,'')
@RemovingCharIndex = PATINDEX('%[^0-9A-Za-z]%',@String)
END
RETURN @String
END
------- Query Sample (untested)---------
SELECT *
FROM PART_TABLE
WHERE DBO.udf_AlphaNumericChars(PARTNO) LIKE '%D12A%'
取自:http : //sqlserver20.blogspot.co.uk/2012/06/find-alphanumeric-characters-only-from.html