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.
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.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.
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.
In SQL Server, you can use the T-SQL CHARINDEX()
function or the PATINDEX()
function to find a string within another string. Here’s a quick overview of each function.
This function accepts 3 arguments; the string to find, the string to search, and an optional start position.
The CHARINDEX()
syntax goes like this:
CHARINDEX ( expressionToFind , expressionToSearch [ , start_location ] )
Where expressionToFind is the expression you want to find in the other string, and expressionToSearch is the other string. The optional start_location can be used to specify a position within expressionToSearch for which to start searching.
Note that only the position of the first occurrence is returned.
In MySQL, you can use the ELT()
function to return an item from a specified position in a list. SQL Server has a similar function, but with a different name.
In SQL Server, the CHOOSE()
function does basically the same thing that the ELT()
function does in MySQL. To be more precise, CHOOSE()
is actually a Transact-SQL function, so it can also be used in Azure databases.
SQL Server has a CHOOSE()
function that returns a list item at a given position. MySQL also has an equivalent function, but with a different name.
In MySQL, the ELT()
function does essentially the same thing that CHOOSE()
does in SQL Server. It allows you to find the list item at a given position within the list.
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.
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
Starting from SQL Server 2012, you can format numeric types using the T-SQL FORMAT()
function. This function accepts three arguments; the number, the format, and an optional “culture” argument.
It returns a formatted string of type nvarchar.
The format is supplied as a format string. A format string defines how the output should be formatted.
Here’s an example:
SELECT FORMAT(1, 'N');
Result:
1.00
In this case, I used N
as the second argument. This is the standard numeric format specifier for Number. This particular format specifier (N
) results in the output being formatted with integral and decimal digits, group separators, and a decimal separator with optional negative sign. This argument is case-insensitive, so either N
or n
is fine.