IF INDEXPROPERTY(Object_Id('ISZL.T_POLICY'), 'IX_T_POLICY_spolnpol', 'IndexID') Is Not Null
BEGIN
    DROP INDEX [IX_T_POLICY_spolnpol] ON [ISZL].[T_POLICY]
END

IF EXISTS (SELECT * FROM sys.columns 
           WHERE object_id = OBJECT_ID('ISZL.T_POLICY') 
           AND name = 'SPOLNPOL')
BEGIN
    ALTER TABLE ISZL.T_POLICY DROP COLUMN SPOLNPOL;
END

ALTER TABLE [ISZL].[T_POLICY]
ADD SPOLNPOL AS 
    CAST (
        CASE 
            WHEN Series IS NULL AND Number IS NULL OR Series = '' and Number = '' THEN ''
            WHEN Series IS NULL OR Series = '' THEN LTRIM(RTRIM(Number))
            WHEN Number IS NULL OR Number = '' THEN LTRIM(RTRIM(Series))
            ELSE LTRIM(RTRIM(Series + CHAR(160) + Number))
        END
    AS VARCHAR(122)) PERSISTED
GO

CREATE INDEX [IX_T_POLICY_spolnpol] ON [ISZL].[T_POLICY] ([SPOLNPOL]);
