Switch-Out a Partition in SQL Server (T-SQL)

In SQL Server, partition switching allows you to load large amounts of data in or out of a table very quickly. This saves you from having to run delete or insert statements, and can be very useful when working with large data sets.

You can use the ALTER TABLE statement to switch a partition in or out of a table.

To switch a partition out of a table, the code goes like this:

ALTER TABLE Table1
SWITCH PARTITION x TO Table2

This switches partition x from Table1 to Table2 (where x is the partition number).

Continue reading

How to Write an ORDER BY Clause with Exceptions using SQL

In SQL, the ORDER BY clause is commonly used to order the results of a query. It allows you to select one or more columns to order the results, and in most cases, it’s probably all you need.

But what if you need to make an exception?

What if you want the results to be ordered alphabetically, except for one row? Or several rows?

Or perhaps you simply want to put any NULL values to the end, while ordering the non-NULL results.

Either way, there’s a neat trick you can use that will enable you to do this. And the good part is, it’s simple.

You can cater for all of the above scenarios by adding a CASE expression to your ORDER BY clause.

Continue reading

Install PostgreSQL on Ubuntu 20.04

Did you know that PostgreSQL is available in all Ubuntu versions by default?

Yes, that means that your Ubuntu 20.04 installation probably already has a PostgreSQL package sitting there waiting for you to install it.

And you can install it with one command, which I’ll show you below.

But what if you prefer to run the latest version of Postgres instead? Well, I’ll also show you how to install that. Installing the latest version is almost as easy as installing your local package.

Continue reading

Install PostgreSQL on Ubuntu 18.04

The following steps demonstrate how to install PostgresSQL on Ubuntu 18.04.

There are a couple of ways to go about installing Postgres on Ubuntu.

But before we start jumping in and installing it, did you know that by default, PostgreSQL is already available in Ubuntu 18.04?

So one option is to go right ahead and use the version that’s already included with Ubuntu.

But unless you have reason not to, you will probably want to run the latest version of PostgreSQL, in which case you’ll want to download and install the latest version.

This tutorial covers both options.

Continue reading

How to Install Azure Data Studio on Ubuntu 20.04

Azure Data Studio is a neat little graphical tool for managing databases.

It’s available on Windows, MacOS, and Linux. On Linux it’s available on Red Hat (RHEL), SUSE Linux Enterprise Server, and Ubuntu.

This tutorial shows you how to install Azure Data Studio on Ubuntu 20.04 via the GUI. Screenshots included.

I’ve also included the commands for installation via the command-line in case you prefer that method.

Continue reading