JSONTABLE – Shredding JSON data into a relational structure

Even though support for XML data has been in the product for a long time now (introduced in SQL 2000 and then improved in SQL 2005), whenever I’m working with it I still find it completely unintuitive and constantly have to refer back to the documentation and previously worked examples. Most of all I rely Read more about JSONTABLE – Shredding JSON data into a relational structure[…]

Building a string of values in T-SQL

It is often far too easy to experience some behaviour in SQL Server, and then extrapolate your belief from that. One example that is seen frequently on forums is the simplistic case of concatenating values into a scalar variable, however the logic that is commonly assumed is false. Take the example USE tempdbGOIF OBJECT_ID(‘CharsTable’,’U’) IS Read more about Building a string of values in T-SQL[…]

Helping the Query Optimizer with Constraints

The Query Optimizer in SQL Server is an amazing piece of code. It takes any query we choose to throw at it, evaluates many different ways of executing that query and, generally speaking, comes up with an execution plan that is going to be good enough. And it does all that in milliseconds. Sometimes it Read more about Helping the Query Optimizer with Constraints[…]

SQL – rounding to nearest 5

Had a requirement to round some price values to the nearest £5 in a SQL stored proc. It’s easy to round to the nearest 10 using a negative value for the length parameter of the ROUND function i.e. [sql]select round(12, -1)[/sql] returns 10 [sql]select round(16, -1)[/sql] returns 20 but length must be of type tinyint, Read more about SQL – rounding to nearest 5[…]

Power and Deception of CTEs

This is a reprint of an article of mine that appeared on SQLServerCentral.com in December 2008 Introduction A customer of mine had a performance problem with a stored procedure that uses Common Table Expressions (CTEs). The performance had gone from sub second to 10-12 secs, and the approach that was being taken to diagnose and Read more about Power and Deception of CTEs[…]