[英]Truncate nvarchar(max) to nvarchar(n) with ellipses
What's the quickest way to convert nvarchar(max) to nvarchar(n) and indicate there's more data? 将nvarchar(max)转换为nvarchar(n)并指示还有更多数据的最快方法是什么?
eg: convert the column Address to nvarchar(100) and if Address is larger than 100 characters then replace the last 3 characters with "..." 例如:将“地址”列转换为nvarchar(100),如果“地址”大于100个字符,则将后3个字符替换为“ ...”
UPDATE t
SET t.Address = SUBSTRING(t.Address, 1, 97) + '...'
FROM TableName t
WHERE LEN(t.Address) > 100;
ALTER TABLE dbo.TableName
ALTER COLUMN Address NVARCHAR (100);
UPDATE Table
SET [Address] = CASE
WHEN LEN([Address]) > 100
THEN CAST([Address] AS NVARCHAR(97)) + N'...'
ELSE CAST([Address] AS NVARCHAR(100))
END
now change the data type of your column since all the data more than 100 characters will be truncated after the above statement. 现在更改列的数据类型,因为以上语句之后所有超过100个字符的数据都将被截断。
ALTER TABLE TableName
ALTER COLUMN [Address] NVARCHAR(100)
GO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.