I have function which I am using in one of the SQL Job (see function below). I am trying to created a persisted column on this function based on existing column on the same table.
It is giving me following error.
Computed column 'FormattedSSN' in table 'SomeTable' cannot be persisted because the column is non-deterministic.
Please suggest if there is any way out. Thanks.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[FormatSSN]
(
@SSN VARCHAR(9)
)
RETURNS CHAR(11)
AS
BEGIN
Declare @FormattedSSN char(11);
IF(LEN(@SSN) = 9)
BEGIN
set @FormattedSSN = SUBSTRING(@SSN, 1, 3) + '-' + SUBSTRING(@SSN, 4, 2) + '-' + SUBSTRING(@SSN, 6,4)
END
ELSE
BEGIN
set @FormattedSSN = @SSN
END
return @FormattedSSN;
END
EDIT: Query used below
ALTER TABLE SomeTable
ADD FormattedSSN as dbo.FormatSSN([EmployeeSSN]) PERSISTED