How to Remove Leading and Trailing Characters in SQL Server

In SQL Server, the TRIM() function is commonly used to remove leading and trailing whitespace from a string. But did you know that you can also remove other characters from the start/end of a string? It doesn’t have to be whitespace.

TRIM() is a T-SQL function that specifically removes the space character char(32) or other specified characters from the start or end of a string.

Continue reading

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

How to Select Everything Before/After a Certain Character in MySQL – SUBSTRING_INDEX()

You can use the MySQL SUBSTRING_INDEX() function to return everything before or after a certain character (or characters) in a string.

This function allows you to specify the delimiter to use, and you can specify which one (in the event that there’s more than one in the string).

Syntax

Here’s the syntax:

SUBSTRING_INDEX(str,delim,count)

Where str is the string, delim is the delimiter (from which you want a substring to the left or right of), and count specifies which delimiter (in the event there are multiple occurrences of the delimiter in the string).

Note that the delimiter can be a single character or multiple characters.

Continue reading