How to Remove Leading Whitespace in MySQL

MySQL has the LTRIM() function that enables you to remove leading whitespace from a string (space at the start of the string).

MySQL also has the TRIM() function that enables you to remove leading space, trailing space (at the end), space from both sides, or to remove other specified characters from either side (or both sides) of the string.

This article demonstrates how to remove leading space using each of these functions.

Continue reading

How to Remove Trailing Whitespace in MySQL

MySQL has an RTRIM() function that enables you to remove trailing whitespace from a string (space at the end of the string).

MySQL also has the TRIM() function that enables you to remove trailing space, leading space (at the start), space from both sides, or to remove other specified characters from either side (or both sides) of the string.

This article demonstrates how to remove trailing space using each of these functions.

Continue reading

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 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