I'm looking for a way to determine if a column contains any non-ASCII data such that I can't convert it from nVarChar to varChar.
Ideally I would like to do this in T-SQL.
I'm looking for a way to determine if a column contains any non-ASCII data such that I can't convert it from nVarChar to varChar.
Ideally I would like to do this in T-SQL.
one simple way is by converting to varchar
select *
from yourtable
where nvarcharcolumn <> cast(nvarcharcolumn as varchar(8000))
The other current answer is missing a step. All it checks is that after converting to varchar
they still compare equal - not that they are actually the same character.
The string N'⁴"*8?'
would silently be converted to '4"*8?'
and still compare equal under many collations.
See
SELECT nvarcharcolumn,
CAST(nvarcharcolumn AS VARCHAR(4000))
FROM ( VALUES(N'⁴"*8?')) V(nvarcharcolumn )
WHERE nvarcharcolumn <> cast(nvarcharcolumn AS VARCHAR(4000))
I would check that it round trips to the exact same value after casting to varchar and back again.You can do this with
WHERE nvarcharcolumn <> cast(nvarcharcolumn as varchar(4000)) collate Latin1_General_100_BIN