In SQL Server, you can use the TRIM()
function to remove leading and trailing whitespace from a string.
TRIM()
is a T-SQL function that removes the space character char(32)
or other specified characters from the start or end of a string.
In SQL Server, you can use the TRIM()
function to remove leading and trailing whitespace from a string.
TRIM()
is a T-SQL function that removes the space character char(32)
or other specified characters from the start or end of a string.
In SQL Server, we can use the RTRIM()
function to remove trailing blanks from a given string. Trailing blanks are white spaces, tabs, etc that come at the end of the string.
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.
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.
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).
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.
In MySQL, the SUBSTRING()
function enables you to return a substring from a string. So you can use this function to grab a portion of text from a larger piece of text.
There are several different ways to use the SUBSTRING()
function, and this affects the syntax.
In SQL Server, you can use the T-SQL SUBSTRING()
function to return a substring from a given string.
You can use SUBSTRING()
to return parts of a character, binary, text, or image expression.
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.
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.