I have a view that is joining a mapping table with a data file and joining on the account (format:11111111-2222-333) string and returning [Entity], [Mapped_Account], [Department]. However, they are explicit mappings and therefore if a exact match for the entire account string doesn't exist, it will return NULL for all columns. I would like to script something that would look at the would take the account sting in parts and and find the match for the individual segments (i.e. -2222- is the entity) if Null is returned.
I can't seem to get the below code to work, so any help would be greatly appreciated.
select account, Dest_Account, Map_Entity, Map_Department, coalesce(Map_Entity, (Select Map_Entity From dbo.VW_MyView where SubString(Source_Account, (CHARINDEX('-', Source_Account, 0) + 1), (CharIndex('-', right(Source_Account, (LEN(Source_Account) - (CharIndex('-', Source_Account, 0))-1)), 0))) = SubString(Account, (CHARINDEX('-', Account, 0) + 1), (CharIndex('-', right(Account, (LEN(Account) - (CharIndex('-', Account, 0))-1)), 0)))) From dbo.[MyView]