Skip to main content
added 113 characters in body
Source Link
Aaron Bertrand
  • 182.2k
  • 28
  • 407
  • 627

You won't be able to index any more than 900 bytes, so with NVARCHAR, that means the first 450 characters. The important point is that using LEFT on its own does not set the destination data type, you need to do that explicitly using CAST or CONVERT, e.g.

ALTER TABLE dbo.Whatever ADD ComputedColumnName AS CONVERT(VARCHARNVARCHAR(500450), COALESCE([fieldValue],'')) PERSISTED; 
ALTER TABLE dbo.Whatever ADD ComputedColumnName AS CONVERT(VARCHAR(500), COALESCE([fieldValue],'')) PERSISTED; 

You won't be able to index any more than 900 bytes, so with NVARCHAR, that means the first 450 characters. The important point is that using LEFT on its own does not set the destination data type, you need to do that explicitly using CAST or CONVERT, e.g.

ALTER TABLE dbo.Whatever ADD ComputedColumnName AS CONVERT(NVARCHAR(450), COALESCE([fieldValue],'')) PERSISTED; 
Source Link
Aaron Bertrand
  • 182.2k
  • 28
  • 407
  • 627

ALTER TABLE dbo.Whatever ADD ComputedColumnName AS CONVERT(VARCHAR(500), COALESCE([fieldValue],'')) PERSISTED;