简体   繁体   English

确定nvarchar列中的varchar内容

[英]Determine varchar content in nvarchar columns

I have a bunch of NVARCHAR columns which I suspect contain perfectly storable data in VARCHAR columns. 我有一堆NVARCHAR列,我怀疑在VARCHAR列中包含完全可存储的数据。 However I can't just go and change the columns' type into VARCHAR and hope for the best, I need to do some sort of check. 但是,我不能只是将列的类型更改为VARCHAR并希望最好,我需要进行某种检查。

I want to do the conversion because the data is static (it won't change in the future) and the columns are indexed and would benefit from a smaller (varchar) index compared to the actual (nvarchar) index. 我想进行转换,因为数据是静态的(将来不会改变)并且列被索引并且与实际(nvarchar)索引相比将受益于更小的(varchar)索引。

If I simply say 如果我只是说

ALTER TABLE TableName ALTER COLUMN columnName VARCHAR(200)

then I won't get an error or a warning. 那我就不会收到错误或警告。 Unicode data will be truncated/lost. Unicode数据将被截断/丢失。

How do I check? 我该如何检查?

Why not cast there and back to see what data gets lost? 为什么不在那里投回去看看哪些数据丢失了?

This assumes column is nvarchar(200) to start with 这假设列的开头是nvarchar(200)

SELECT *
FROM TableName
WHERE columnName <> CAST(CAST(columnName AS varchar(200)) AS nvarchar(200))

Hmm interesting. 嗯有趣。

I'm not sure you can do this in a SQL query itself. 我不确定你是否可以在SQL查询中做到这一点。 Are you happy to do it in code? 你是否乐于在代码中做到这一点? If so, you can get all the records, then loop over all the chars in the string and check. 如果是这样,您可以获取所有记录,然后遍历字符串中的所有字符并检查。 But man it's a slow way. 但是男人这是一个缓慢的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM