什么是SQL Server TRIM()函数?

Hello, readers. In today’s article, we will be focusing on SQL Server TRIM() function in detail.

您好,读者们。 在今天的文章中,我们将重点关注SQL Server TRIM()函数



SQL Server TRIM()函数的工作 (Working of SQL Server TRIM() function)

In every programming language or query language, a set of functions are specified to manipulate and deal with the data input values.

在每种编程语言或查询语言中,都指定了一组函数来操纵和处理数据输入值。

On similar lines, SQL Server has in-built functions to deal with the String data values.

类似地,SQL Server具有内置函数来处理String数据值。

SQL Server TRIM() function helps eliminate the leading and trailing spaces from the data values contained in a table of the database.

SQL Server TRIM()函数有助于从数据库表中包含的数据值中消除前导和尾随空格

The TRIM() function not only eliminates the white spaces but also deletes a particular character from the leading and trailing part of the string values.

TRIM()函数不仅消除了空格,而且还从字符串值的开头和结尾部分删除了特定字符。

Now let us understand the syntax of TRIM() function in the upcoming section.

现在,让我们在接下来的部分中了解TRIM()函数的语法。



SQL Server TRIM()函数的语法 (Syntax of SQL Server TRIM() function)

SQL Server TRIM() function accepts the data to be trimmed and the character value( in case we want to trim certain characters from the string data).

SQL Server TRIM() function接受要修剪的数据和字符值(以防我们要从字符串数据中修剪某些字符)。


SELECT TRIM('characters' FROM 'data-value');
  • characters (OPTIONAL): Set of characters are passed which we want to eliminate from the starting and ending point of the string data values.

    characters (OPTIONAL) :传递的字符集我们要从字符串数据值的起点和终点消除。

Having understood the structure of TRIM() function, let us understand the implementation of the same through some examples.

了解了TRIM() function的结构之后,让我们通过一些示例来了解其实现。



TRIM()函数的示例 (Examples of TRIM() function)

In the below example, we intend to trim the character ‘$’ from the input data value.

在下面的示例中,我们打算从输入数据值中修剪字符“ $”。


SELECT TRIM('$ ' FROM '   $PythonJournalDev$   ') AS Output;

As clearly seen below, the TRIM() function has completely eliminated ‘$’ from the leading and trailing part of the string.

如下所示,TRIM()函数已从字符串的开头和结尾部分完全消除了“ $”。

Output:

输出:


PythonJournalDev

TRIM() function detects and eliminates all the leading and trailing white-spaces too as seen below.

TRIM()函数也可以检测并消除所有前导和尾随空格,如下所示。


SELECT TRIM('   $PythonJournalDev$   ') AS Output;

In the above example, the character ‘$’ does not get eliminated as we have not passed the character to the parameter list. Here, the TRIM() function eliminates the spaces here.

在上面的示例中,字符“ $”没有被消除,因为我们没有将字符传递给参数列表。 在这里,TRIM()函数消除了此处的空格。

Output:

输出:


$PythonJournalDev$

In this example, we have created a table and inserted values into it. Further, we have used TRIM() function to trim the white spaces from all the data values of the passed column.

在此示例中,我们创建了一个表并将值插入其中。 此外,我们使用了TRIM()函数从传递的列的所有数据值中修剪空白。


CREATE TABLE Info (
    id INT PRIMARY KEY,
    City VARCHAR (255) NOT NULL
); 

INSERT INTO Info (
   id,
   City
)
VALUES
    (
        1,
      '  Pune  '
    ),
    (
        2,
      '       Satara  '
    ),
    (
        3,
      ' California       '
    );
UPDATE 
    Info
SET  
    City = TRIM(City);
    
Select * from Info;

Output:

输出:


id	City
1	Pune
2	Satara
3	California


结论 (Conclusion)

That’s all for this topic. Please feel free to comment below in case you come across any doubt.

这就是本主题的全部内容。 如果您有任何疑问,请随时在下面发表评论。

For more such posts related to SQL Server, please do visit SQL Server JournalDev.

有关与SQL Server相关的更多此类帖子,请访问SQL Server JournalDev



参考资料 (References)

翻译自: https://www.journaldev.com/41172/sql-server-trim-function

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值