How to Select a Certain Number of Characters from the Left or Right of a String in SQL Server

If you ever find yourself wanting only the first part of a string, or the last part of it, this article might help.

This article is specific to SQL Server, but the functionality is pretty common across most/all database management systems, not to mention most programming languages.

When working with SQL Server, you can use theT-SQL LEFT() and RIGHT() functions to return any given number of characters from the left or right of a string.

Continue reading

How to Return the Left or Right Part of a String in MySQL

When working with MySQL databases, you might occasionally find yourself needing to select only a certain number of characters from the left or right of a string. In such cases, you can use the LEFT() and RIGHT() functions to do just that.

Here’s the syntax for both of these functions:

LEFT(str,len)
RIGHT(str,len)

Where str is the string that contains the substring you need to return, and len is the number of characters from the left you want returned.

Continue reading

How to Return a String in Reverse Order using SQL Server – REVERSE()

Starting from SQL Server 2008, the REVERSE() function can be used to reverse the order of a string. That is, it returns the characters in the string in reverse order.

Here’s the syntax:

REVERSE ( string_expression )

Where string_expression is an expression of a string or binary data type. It can be a constant, variable, or column of either character or binary data.

Continue reading

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