Skip to content
Advertisement

Is it possible to use dynamic columns multiple times in a select?

I have some complex string replacement and I’m only permitted read-only access.

I’d like to have several columns where each is dependent on the previous column so I can see if my substring and charindex functions are correct.

Basically this:

Where I would want the output of the second column to be:

ThisIsOurTest added to the end

This is an extremely simplified version of what I’m trying to accomplish, so I’m looking for the concept. I don’t need this answer:

as that’s what I’m doing now.

Advertisement

Answer

In SQL Server, one option uses lateral joins. They are handy in this situation, because the “next” join has access to the dataset generated by the previous one(s). Basically, you can put each intermediate computation in a separate (scalar) subquery.

Starting from your simplified example, that would be:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement