• OK, SQL Agent data, that's good, the use case fits, but you're repurposing it so how much data are we talking about? For managing SQL Agent historical data the scalar-function is fine because the volume is limited (if the instance is configured well) but if we're talking about a non-trivial amount of data you should consider switching to a home-made table-valued function instead. It will scale much better and not use anywhere near as much CPU.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato