开发者

how can i use LRTIM and RTRIM to find invalid records and insert it into error table?

I have a query that finds the invalid records from each column in a table. but the length of the data type is not same for all the columns. I am saying this because when I use LTRIM and RTRIM , it gives an error "The type of column "SubscriberLastName" conflicts with the type of other columns specified in the UNPIVOT list" and if I CAST that column then no result.

Can anybody help me with that?

Insert into ErrorTable (------) select (-----)
From (
            select [SubscriberDataId]
            ,Case When ltrim(rtrim([SubscriberCode])) = '' Then [SubscriberCode] Else 'CorrectValue' end as [SubscriberCode]
            ,Case When ltrim(rtrim([SubscriberLastName]))= '' Then [SubscriberLastName] Else 'CorrectValue' end as [SubscriberLastName]
            ,Case When ltrim(rtrim([Sub开发者_StackOverflow中文版scriberFirstName]))= ''  Then [SubscriberFirstName] Else 'CorrectValue' end as [SubscriberFirstName]
            from Facets.SubscriberData) [sd]
            Unpivot
            (ErrorValue for FieldName in ([SubscriberCode],
            [SubscriberLastName],[SubscriberFirstName] )) as x
            where x.ErrorValue <> 'CorrectValue'


You need to make sure the TYPEs are the same, not just the lengths.

Insert into ErrorTable (------) select (-----)
From (
            select [SubscriberDataId]
            ,Case When ltrim(rtrim([SubscriberCode])) = '' Then CAST([SubscriberCode] AS NVARCHAR(MAX)) Else 'CorrectValue' end as [SubscriberCode]
            ,Case When ltrim(rtrim([SubscriberLastName]))= '' Then CAST([SubscriberLastName] AS NVARCHAR(MAX)) Else 'CorrectValue' end as [SubscriberLastName]
            ,Case When ltrim(rtrim([SubscriberFirstName]))= ''  Then CAST([SubscriberFirstName] AS NVARCHAR(MAX)) Else 'CorrectValue' end as [SubscriberFirstName]
            from Facets.SubscriberData) [sd]
            Unpivot
            (ErrorValue for FieldName in ([SubscriberCode],
            [SubscriberLastName],[SubscriberFirstName] )) as x
            where x.ErrorValue <> 'CorrectValue'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜