This is a static query that kind of shows where I am trying to get
declare @Airbill VARCHAR(50) = '12345678912'
select stuff(stuff(@Airbill, 4, 0, '-'), 9, 0, '-')
But I need to make it more variable. I have the following varible:
DECLARE @AirbillMask VARCHAR(50) = '999-9999-9999'
Which can (and does) change. It is always '9's and '-'s. But the placement of them changes.
- If
@AirbillMaskis999-99-99-9999my desired result would be123-45-67-8912. - If
@AirbillMaskis999-9999-9999my desired result would be123-4567-8912.
I am running dbo.FindPatternLocation(@AirbillMask, '-') that returns a table of dash locations.
I was planning to find a way to STUFF the parts of the @Airbill into it corresponding locations to produce a calculated field of FormattedAirbill, but I got stuck trying to iterate down into the STUFF calls.
Is there a way to call STUFF inside another STUFF call, over and over till I am out of rows?
I feel like a recursive CTE or cross apply could accomplish this, but I can't seem to wrap my head around it.
NOTE: I would prefer to do this without looping, if possible.