Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

An interesting customer question explained

While at a customer today I was given an interesting question. The individual was writing reports and needed to have right-justified, zero padded fields. He was investigating the Format Function which was introduced in SQL Server 2012. He had heard some mumblings that the FORMAT function was not as performant as good ole number stuffing. He wanted to understand why.

So basically, he wanted a cook-off between

SELECT FORMAT(N, 'd10') as padWithZeroes FROM FormatTest;

and

SELECT RIGHT('0000000000'+RTRIM(CAST (N as varchar(5))),10) from FormatTest;

I had a hunch but wanted empirical evidence...

so fire up ssms and our trusty Query Store and create a repro.

[snippet slug=format-vs-stuff lang=sql]

So now to examine the Query Store runtime execution statistics

[snippet slug=query-store-query lang=sql]

And as I suspected FORMAT is utilizing the SQL CLR services as we see almost all the time being spent in CLR. Question answered.

Share the post

An interesting customer question explained

×

Subscribe to Msdn Blogs | Get The Latest Information, Insights, Announcements, And News From Microsoft Experts And Developers In The Msdn Blogs.

Get updates delivered right to your inbox!

Thank you for your subscription

×