How to Remove Leading Whitespace in SQL Server

Leading whitespace is a common issue when working with data. A leading whitespace is a space at the start of a string. In most cases, we don’t want any leading whitespace, and we will want to remove it before the data goes any further (whether that means being stored in the database, displayed to the user, or whatever).

Fortunately, SQL Server provides us with the LTRIM() function that allows us to remove leading blanks from a given string.

Continue reading

2 Ways to Select Rows that Match all Items in a List (T-SQL)

This article presents two ways to select rows based on a list of IDs (or other values) in SQL Server. This can be useful in scenarios where you have a comma-separated list of IDs, and you want to query your database for rows that match those IDs.

Say you have the following list of IDs:

1,4,6,8

And so you now want to query a table for records that have any of those values (i.e. either 1, 4, 6 or 8) in its ID column.

Here are two ways to go about that.

Continue reading

How to Convert a Comma-Separated List into Rows in SQL Server

So you have a comma-separated list, and now you need to insert it into the database. But the thing is, you need to insert each value in the list into its own table row. So basically, you need to split the list into its separate values, then insert each one of those values into a new row.

T-SQL now has a STRING_SPLIT() function that makes this type of operation a breeze. This function was first available in SQL Server 2016, and is available on databases with a compatibility level of 130 or above (how to check your database compatibility level and how to change it).

Continue reading

How to Check a Database’s Compatibility Level in SQL Server using T-SQL

In SQL Server, you can use T-SQL to check the compatibility level of a database. All you need to do is query sys.databases to find the compatibility level for the database in question.

Here’s an example:

SELECT compatibility_level
FROM sys.databases
WHERE name = 'WideWorldImporters';

Result:

compatibility_level
-------------------
130                

This example returns the compatibility level of the WideWorldImporters database.

Continue reading

What’s the MySQL Equivalent of STUFF() in SQL Server?

If you’re familiar with SQL Server,  you might know that you can use the T-SQL STUFF() function to insert a string within a string.  As it turns out, MySQL has a similar function – but with a different name.

MySQL’s INSERT() function does essentially the same thing that the T-SQL STUFF() function does.

With the exception of a couple of minor differences (see below), both functions work exactly the same.

Continue reading

How to Insert a String into another String in SQL Server using STUFF()

In SQL Server, you can use the T-SQL STUFF() function to insert a string into another string. This enables you to do things like insert a word at a specific position. It also allows you to replace a word at a specific position.

Here’s the official syntax:

STUFF ( character_expression , start , length , replaceWith_expression )
  • character_expression is the original string. This can actually be a constant, variable, or column of either character or binary data.
  • start specifies the start position (i.e. where the new string will be inserted).
  • length is how many characters are to be deleted from the original string.
  • replaceWith_expression is the string that’s being inserted. replaceWith_expression can be a constant, variable, or column of either character or binary data.

Continue reading

What is T-SQL?

Transact-SQL, often abbreviated to T-SQL or even TSQL, is Microsoft’s and Sybase’s proprietary extension to SQL. Transact-SQL expands on the SQL standard to include extra features that aren’t included in the SQL standard.

Here’s an example of a simple T-SQL statement:

CREATE DATABASE Movies;

This is as simple as a T-SQL example could get. This creates a new database called Movies.

However, T-SQL provides for other options to be included in this statement, such as where the database files should be located, the size of those files, what their maximum size should be, and more.

Continue reading

CHARINDEX() vs PATINDEX() in SQL Server – What’s the Difference?

In SQL Server, you can use either the CHARINDEX() function or the PATINDEX() function to find a string within a string. These are Transact-SQL string functions, and they’re also available on Azure databases.

On the surface, these functions appear to do exactly the same thing, and in many cases, you could use whichever you prefer to use.

However, there a a couple of distinctions that could dictate which function you decide to use in certain scenarios. These can be summarized by the following:

  • PATINDEX() allows you to use wildcard characters to search for patterns. CHARINDEX() doesn’t.
  • CHARINDEX() accepts a third argument which allows you to specify the start position of the search. PATINDEX() doesn’t.

More detail on these points below.

Continue reading

How to Find a List Item at a Specified Position in SQL Server

Starting from SQL Server 2012, you can use the T-SQL CHOOSE() function to find a list item at a specified index position within a list.

The syntax goes like this:

CHOOSE ( index, val_1, val_2 [, val_n ] )

Where index is an integer that represents the position within the list that you want to return.

Example

Here’s an example:

SELECT CHOOSE(3, 'Marge', 'Homer', 'Bart') AS 'Who is at 3?';

Result:

Who is at 3?
------------
Bart        

In this case, we want to find the item at position 3. The item at position 3 is Bart.
Continue reading