How to Convert JSON to Rows and Columns in SQL Server

Modern applications often exchange information in JSON, and that data often ends up in SQL Server. While JSON’s flexible structure makes it ideal for storing dynamic or nested data, it doesn’t fit neatly into traditional relational tables. The good news is that SQL Server includes a good selection of JSON functions that let you parse, query, and transform JSON content into structured rows and columns. This means that you can work with your JSON data just like any other table.

Read more

What is a Materialized View?

A materialized view is a database object that stores the results of a query physically on disk, rather than computing them on the fly every time you need them. It’s basically a snapshot of your query results that you can refresh periodically. Unlike regular views (which are just saved queries that execute each time you use them), materialized views pre-compute and cache the data, making subsequent reads much faster.

Read more

What is a Cursor in SQL?

If you’ve been working with SQL for a while, you’ve probably heard someone mention cursors, usually followed by a warning to avoid them. Maybe you’ve used them yourself. But what exactly are cursors, and why do they get such a bad rap? Let’s take a look at what cursors are, how they work, and when (if ever) you should actually use them.

Read more

Calculating Past Dates in SQL Server

Working with dates is one of those things you’ll do constantly in SQL Server, and knowing how to calculate past dates efficiently can save you a ton of time. Whether you’re pulling last month’s sales data or filtering records from the past week, SQL Server gives you several straightforward ways to handle date calculations.

Read more

What is a Derived Table in SQL?

If you’ve been writing SQL for a while, you’ve probably run into situations where you need to query data that’s already been aggregated, filtered, or transformed in some way. Maybe you need to calculate an average first, then find all the rows above that average. Or perhaps you need to group data by category, then filter those grouped results.

This is where derived tables can come in handy. Derived tables let you build queries in layers, where one query’s output becomes another query’s input, all within a single SQL statement.

Read more

What is a Temp Table?

Need a high-performance, disposable workspace for your SQL queries? Meet temporary tables, also known simply as temp tables. As their name implies, these tables offer a short-lived storage solution, ideal for holding intermediate data or simplifying complex multi-step processing. They exist just long enough to get the job done, providing a handy scratch pad that vanishes automatically to keep your database clean.

Read more

Using CAST() to Convert Rounded Values to Integers in SQL Server

Sometimes when you’re working with calculated columns in SQL Server, you might get results in a data type that’s less than ideal. For example, maybe it’s a decimal or float when you really need an integer. This can result in the output not appearing exactly the way you want, such as with a bunch of unnecessary trailing decimal zeros.

In such cases, you’ll probably want to remove these trailing zeros from the result. The CAST() function is perfect for this kind of cleanup. You can use it to convert the value to a more suitable type.

Read more

How to Conditionally Drop Objects Before Recreating Them in SQL Server

Recreating objects like tables, views, stored procedures, or functions is quite common when developing databases. Maybe you’re iterating on a design, maybe you’re fixing a bug, or maybe you just need a clean slate. The problem is that SQL Server will throw an error if you try to create an object that already exists. To avoid that, you’ll need a reliable way to conditionally drop the object before recreating it.

And this isn’t just a matter of convenience. It helps keep scripts idempotent, meaning you can run them multiple times without worrying about errors or leftover objects from previous runs.

Fortunately SQL Server provides us with at least two easy options for doing this.

Read more

What is a Rollback in SQL?

In SQL, a rollback is a command that reverses all the changes made during a transaction. When you execute a ROLLBACK statement, the database management system undoes all the Data Manipulation Language (DML) operations (such as INSERT, UPDATE, and DELETE) that happened since the transaction began (or since a specified savepoint), restoring the database to its previous consistent state.

Read more