sql trim函数_SQL TRIM函数

sql trim函数

In this article, we will review the new SQL TRIM function in SQL Server 2017 onwards as well as providing some information on strings functions that pre-date it like LTRIM AND RTRIM.

在本文中,我们将回顾SQL Server 2017及更高版本中的新SQL TRIM函数,并提供有关早于它的字符串函数(如LTRIM和RTRIM)的一些信息。

SQL Developers usually face issueS with spaceS at the beginning and/or end of a character string. We may need to trim leading and trailing characters from a character string. We might need to do string manipulations with SQL functions as well. Suppose we want to remove spaces from the trailing end of a string, we would need to use SQL LTRIM and RTRIM functions until SQL Server 2016.

SQL开发人员通常在字符串的开头和/或结尾面临带有空格的问题。 我们可能需要修剪字符串中的前导和尾随字符。 我们可能还需要使用SQL函数进行字符串操作。 假设我们要从字符串的尾部删除空格,则需要在SQL Server 2016之前使用SQL LTRIM和RTRIM函数。

In SQL Server 2017, we get a new built-in function to trim both leading and trailing characters together with a single function. SQL TRIM function provides additional functionality of removing characters from the specified string. We can still use RTRIM and LTRIM function with SQL Server 2017 as well. Let’s explore these functions with examples.

在SQL Server 2017中,我们获得了一个新的内置函数,可以使用单个函数修剪前导字符和尾随字符。 SQL TRIM函数提供了从指定字符串中删除字符的附加功能。 我们仍然可以在SQL Server 2017中使用RTRIM和LTRIM函数。 让我们通过示例探索这些功能。

SQL LTRIM函数 (SQL LTRIM function)

It removes characters from beginning (Starting from the left side) of the specified string. In the following query, we have white space before and after the string. We need to remove space from the left side of the string using the LTRIM function.

它从指定字符串的开头(从左侧开始)删除字符。 在以下查询中,字符串前后有空格。 我们需要使用LTRIM函数从字符串的左侧删除空格。

We use the SQL DATALENGTH() function to calculate data length in bytes before and after using SQL LTRIM function.

在使用SQL LTRIM函数之前和之后,我们使用SQL DATALENGTH()函数来计算数据长度(以字节为单位)。

DECLARE @String VARCHAR(26)= '     Application       ';
SELECT @String as OriginalString, 
       LTRIM(@String) AS StringAfterTRIM, 
       DATALENGTH(@String) AS 'DataLength String (Bytes)', 
       DATALENGTH(LTRIM(@String)) AS 'DataLength String (Bytes) After TRIM';

sql trim函数_SQL TRIM函数_第1张图片
  • Data String size in Bytes for Original String: 24

    原始字符串的数据字符串大小(以字节为单位):24
  • Data String size in Bytes after SQL LTRIM: 18

    SQL LTRIM之后的数据字符串大小(以字节为单位):18

SQL RTRIM函数 (SQL RTRIM function)

It removes characters from the end ((Starting from the right side) of the specified string. Let’s execute the following query to look at the effect of SQL RTRIM function.

它从指定字符串的末尾((从右侧开始)删除字符。让我们执行以下查询以查看SQL RTRIM函数的效果。

DECLARE @String VARCHAR(26)= '     Application         ';
SELECT @String as OriginalString, 
       RTRIM(@String) AS StringAfterTRIM, 
       DATALENGTH(@String) AS 'DataLength String (Bytes)', 
       DATALENGTH(RTRIM(@String)) 

你可能感兴趣的:(字符串,python,java,数据库,mysql)