简体   繁体   中英

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?

eg: convert the column Address to nvarchar(100) and if Address is larger than 100 characters then replace the last 3 characters with "..."

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.

ALTER TABLE TableName
ALTER COLUMN [Address] NVARCHAR(100)
GO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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