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

Handling Month Names in Different Languages in SQL Server

If your database serves users in different regions, controlling how month names appear is one of those small but important details. Maybe you’re generating reports for users across regions, or exporting data that needs to match a specific locale. Whatever the case, sometimes you just need SQL Server to show month names in a different language.

This article walks through how SQL Server handles month names under different language and locale settings, and how you can control that behavior.

Read more

Displaying Abbreviated and Full Day Names for Reports in SQL Server

When building reports in SQL Server, dates are probably one of the most common pieces of data you’ll deal with. Sometimes a report needs the full day name like “Monday”, while in other cases a short form like “Mon” is preferred, often to save space. Luckily, SQL Server has built-in functionality to handle both, without having to manually map numbers to names.

Let’s look at how we can display abbreviated and full day names in queries so that our reports are nice and easy to read.

Read more

Using “GO” to Structure T-SQL Batches

If you spend much time writing T-SQL scripts, you’ve probably seen the GO keyword. It looks like a T-SQL command, but it’s not really part of T-SQL. Instead, it’s a batch separator recognized by SQL Server Management Studio (SSMS) and other client tools. When you hit the “Execute” button, any GO keyword in your script tells the tool that this is the end of a batch, and to send what came before it to SQL Server as one unit.

Many scripts will run fine without the GO keyword, but others will fail miserably. Understanding how GO works can save you from frustrating errors and unexpected behavior.

Read more

How to Identify Foreign Keys with Cascade Action in SQL Server

When working with relational databases in SQL Server, foreign keys help maintain referential integrity between tables. Sometimes these constraints are defined with actions such as CASCADE, which automatically propagates updates or deletions from a parent table to related rows in a child table.

While this functionality can be useful, it can also introduce unexpected side effects if you are not aware of where it is enabled. Knowing how to identify foreign keys with cascade actions is an important part of understanding data dependencies, troubleshooting issues, and ensuring database operations behave as intended.

Read more

Detecting and Filtering Special Characters Using PATINDEX() and LIKE in SQL Server

Working with real-world data often means dealing with messy strings. It’s common to find values that contain unexpected special characters. Sometimes this is due to user input, sometimes it’s from imports or third-party sources.

Either way, when we need to find and filter these special characters, SQL Server gives us some handy tools to work with. For starters, there’s the LIKE operator, which anyone who’s used SQL would be familiar with. But there’s also the PATINDEX() function, which performs a slightly different task.

Read more

Writing Valid ORDER BY Queries for Views and CTEs in SQL Server

If you’ve worked with SQL Server long enough, you’ve probably hit the dreaded error when trying to use ORDER BY inside a view or CTE. It usually shows up as something like:

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.

This rule can come as a bit of a surprise because it feels natural to want a query “pre-sorted” when wrapping it in a view or CTE. The problem is that SQL Server doesn’t allow the ORDER BY clause in this context unless it’s in conjunction with the clauses mentioned in the error message. Without such clauses, you need to explicitly request it at the outermost SELECT.

Let’s walk through an example of how to handle this.

Read more

How to Prevent Overflow Errors When Aggregating Data in SQL Server

When working with aggregate functions in SQL Server, it’s easy to overlook that certain datatypes have strict limits. If you’re summing values in large tables (or even just summing very large numbers), you might run into an arithmetic overflow error. This happens when the result of an aggregate exceeds the maximum value the datatype can hold.

Understanding how this works and how to prevent errors will help you write reliable queries.

Read more

How to Use Window Functions to Find Duplicates in SQL Server

When you’re working with real-world data, duplicates can sometimes sneak in more often than we’d like. Maybe an import process didn’t filter things properly, or users managed to submit the same information twice. Whatever the reason, finding and handling duplicates is a common task.

A neat way to tackle this problem in SQL Server is by using window functions. Specifically, we can use ROW_NUMBER() or RANK() to find duplicate rows in a table. These functions let you assign a sequence number to rows within a group, which makes it easy to spot duplicates.

Read more