SQL LTRIM Function

Example

Remove leading spaces from a string:

SELECT LTRIM('     SQL Tutorial') AS LeftTrimmedString;

Definition and Usage

The LTRIM() function removes leading spaces from a string.

Note: Also look at the RTRIM() function.

Syntax

LTRIM(string)

Continue reading SQL LTRIM Function

SQL LOWER Function

Example

Convert the text to lower-case:

SELECT LOWER('SQL Tutorial is FUN!');

Definition and Usage

The LOWER() function converts a string to lower-case.

Note: Also look at the UPPER() function.

Syntax

LOWER(text)

Continue reading SQL LOWER Function

SQL LEN Function

Example

Return the length of a string:

SELECT LEN('Iampsp.com');

Definition and Usage

The LEN() function returns the length of a string.

Note: Trailing spaces at the end of the string is not included when calculating the length. However, leading spaces at the start of the string is included when calculating the length.

Tip: Also look at the DATALENGTH() function.

Syntax

LEN(string)

Continue reading SQL LEN Function

SQL LEFT Function

Example

Extract 3 characters from a string (starting from left):

SELECT LEFT('SQL Tutorial', 3) AS ExtractString;

Definition and Usage

The LEFT() function extracts a number of characters from a string (starting from left).

Syntax

LEFT(string, number_of_chars)

Continue reading SQL LEFT Function

SQL FORMAT Function

Example

Format a date:

DECLARE @d DATETIME = '12/01/2018';
SELECT FORMAT (@d, 'd', 'en-US') AS 'US English Result',
               FORMAT (@d, 'd', 'no') AS 'Norwegian Result',
               FORMAT (@d, 'd', 'zu') AS 'Zulu Result';

Definition and Usage

The FORMAT() function formats a value with the specified format (and an optional culture in SQL Server 2017).

Use the FORMAT() function to format date/time values and number values. For general data type conversions, use CAST() or CONVERT().

Syntax

FORMAT(value, format, culture)

Continue reading SQL FORMAT Function

SQL Server DIFFERENCE Function

Example

Compares two SOUNDEX values, and return an integer:

SELECT DIFFERENCE('Juice', 'Jucy');

Definition and Usage

The DIFFERENCE() function compares two SOUNDEX values, and returns an integer. The integer value indicates the match for the two SOUNDEX values, from 0 to 4.

0 indicates weak or no similarity between the SOUNDEX values. 4 indicates strong similarity or identically SOUNDEX values.

 

Syntax

DIFFERENCE(expression, expression)

Continue reading SQL Server DIFFERENCE Function

SQL Server DATALENGTH Function

Example

Return the length of an expression (in bytes):

SELECT DATALENGTH('W3Schools.com');

Definition and Usage

The DATALENGTH() function returns the number of bytes used to represent an expression.

Note: The DATALENGTH() function counts both leading and trailing spaces when calculating the length of the expression.

 

Syntax

DATALENGTH(expression)

Continue reading SQL Server DATALENGTH Function

SQL Server CONCAT_WS Function

Example

Add strings together. Use ‘.’ to separate the concatenated string values:

SELECT CONCAT_WS('.', 'www', 'iampsp', 'com');

Definition and Usage

The CONCAT_WS() function adds two or more strings together with a separator.

 

Syntax

CONCAT_WS(separator, string1, string2, ...., string_n)

Continue reading SQL Server CONCAT_WS Function

SQL Server Concat With +

Example

Add 2 strings together:

SELECT 'iampsp' + '.com';

Definition and Usage

The + operator allows you to add two or more strings together.

 

Syntax

string1 + string2 + string_n

Continue reading SQL Server Concat With +

SQL Server CONCAT Function

Example

Add two strings together:

SELECT CONCAT('iampsp', '.com');

Definition and Usage

The CONCAT() function adds two or more strings together.

 

Syntax

CONCAT(string1, string2, ...., string_n)

Continue reading SQL Server CONCAT Function