I've been looking at a query that looks for non-ASCII characters, reproduced below…
select line,
patindex('%[^ !-~]%' COLLATE Latin1_General_BIN, Line) as [Position],
substring(Line, patindex('%[^ !-~]%' COLLATE Latin1_General_BIN, Line), 1) as [InvalidCharacter],
ascii(substring(line, patindex('%[^ !-~]%' COLLATE Latin1_General_BIN, Line), 1)) as [ASCIICode]
from staging.APARMRE1
where patindex('%[^ !-~]%' COLLATE Latin1_General_BIN, Line) > 0
…and it just strikes me that I'd want to declare a variable for '%[^ !-~]%' COLLATE Latin1_General_BIN
instead of writing it out every time, but
declare @regex varchar(20) = '%[^ !-~]%' COLLATE Latin1_General_BIN;
select line,
patindex(@regex, Line) as [Position],
substring(Line, patindex(@regex, Line), 1) as [InvalidCharacter],
ascii(substring(line, patindex(@regex, Line), 1)) as [ASCIICode]
from staging.APARMRE1
where patindex(@regex, Line) > 0
just doesn't do the same thing. Am I just missing some syntax? Is it impossible?