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';
- 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)) AS 'DataLength String (Bytes) After TRIM';
- Data String size in Bytes for Original String: 24 原始字符串的數據字符串大小(以字節為單位):24
- Data String size in Bytes after SQL RTRIM: 16 SQL RTRIM之后的數據字符串大小(以字節為單位):16
SQL Server 2017中的TRIM函數 (TRIM function in SQL Server 2017)
Before SQL Server 2017, if we need to remove both Leading and Training spaces, we need to use both LTRIM and RTRIM function. In the following query, we used both LTRIM and RTIM functions with a string variable.
在SQL Server 2017之前,如果需要同時刪除Leading和Training空間,則需要同時使用LTRIM和RTRIM函數。 在以下查詢中,我們將LTRIM和RTIM函數與字符串變量一起使用。
DECLARE @String VARCHAR(26)= '???? Application???????? '; SELECT @String as OriginalString, LTRIM(RTRIM(@String)) AS StringAfterTRIM, DATALENGTH(@String) AS 'DataLength String (Bytes)', DATALENGTH(LTRIM(RTRIM(@String))) AS 'DataLength String (Bytes) After TRIM';In the following screenshot, we can see it removes both leading and trailing space from the string.
在下面的屏幕截圖中,我們可以看到它刪除了字符串中的前導和尾隨空格。
- Data String size in Bytes for Original String: 24 原始字符串的數據字符串大小(以字節為單位):24
- Data String size in Bytes after SQL LTRIM: 11 SQL LTRIM之后的數據字符串大小(以字節為單位):11
- Data String size in Bytes for Original String: 25 原始字符串的數據字符串大小(以字節為單位):25
- Data String size in Bytes after SQL LTRIM and RTRIM: 11 SQL LTRIM和RTRIM之后的數據字符串大小(以字節為單位):11
We need to use two functions to remove the spaces from the string. We can use a single instance of a TRIM function in SQL Server 2017 onwards to trim both leading and trailing characters, let’s explore the TRIM function in a further section of this article.
我們需要使用兩個函數從字符串中刪除空格。 我們可以在SQL Server 2017及更高版本中使用TRIM函數的單個實例來修剪前導和尾隨字符,讓我們在本文的下一部分中探索TRIM函數。
TRIM函數的語法 (The Syntax of the TRIM Function)
TRIM ([Trimcharacters FROM] string)- TrimCharacter: We can specify the character to remove from the string. By default, SQL Server removes spaces from both sides of a string TrimCharacter :我們可以指定要從字符串中刪除的字符。 默認情況下,SQL Server從字符串的兩側刪除空格
- String: We need to specify the string that we want to trim 字符串:我們需要指定要修剪的字符串
Let’s explore SQL TRIM function with examples.
讓我們通過示例探索SQL TRIM函數。
TRIM領導和培訓空間: (TRIM Leading and Training spaces:)
By default, Trim function removes the space from both sides of a string. In the following query, we have to define a string with space on both sides of the string.
默認情況下,Trim函數會刪除字符串兩側的空格。 在下面的查詢中,我們必須定義一個字符串,字符串的兩邊都有空格。
Execute this query:
執行此查詢:
DECLARE @String VARCHAR(24)= '??????Application?????? '; SELECT @String as OriginalString, TRIM(@String) AS StringAfterTRIM, DATALENGTH(@String) AS 'DataLength String (Bytes)', DATALENGTH(TRIM(@String)) AS 'DataLength String (Bytes) After TRIM';We did not specify any characters to remove in TRIM function. In the output, SQL TRIM function removes both leading and trailing space.
我們沒有在TRIM函數中指定要刪除的任何字符。 在輸出中,SQL TRIM函數將刪除前導和尾隨空格。
You can also notice the difference in string length before and after the TRIM function.
您還可以注意到TRIM函數前后字符串長度的差異。
- Data String size in Bytes for Original String: 24 原始字符串的數據字符串大小(以字節為單位):24
- Data String size in Bytes after SQL TRIM: 11 SQL TRIM之后的數據字符串大小(以字節為單位):11
TRIM字符串中的前導字符: (TRIM Leading characters from a string:)
In the previous example, we explored that SQL TRIM is the replacement of RTRIM and LTRIM and it eliminates to delete all space character before and after a string. We can remove the characters as well from the string using TRIM. Suppose we want to remove character ON from the string, we can specify it in TRIM function as per following script.
在前面的示例中,我們探討了SQL TRIM是RTRIM和LTRIM的替代品,它消除了刪除字符串前后的所有空格字符。 我們也可以使用TRIM從字符串中刪除字符。 假設我們要從字符串中刪除字符ON ,可以按照以下腳本在TRIM函數中指定它。
DECLARE @String VARCHAR(24)= 'Application'; SELECT @String as OriginalString, TRIM('On' from @String) AS StringAfterTRIM, DATALENGTH(@String) AS 'DataLength String (Bytes)', DATALENGTH( TRIM('On' from @String) ) AS 'DataLength String (Bytes) After TRIM';It checks the specified characters on both the leading and trailing side and removes the particular characters. In the string Application, we have character ON in the leading side. In the following screenshot, you can look at the output after TRIM function.
它在開頭和結尾都檢查指定的字符,并刪除特定的字符。 在字符串Application中,我們在開頭將字符ON。 在下面的屏幕快照中,您可以查看TRIM功能之后的輸出。
- Data String size in Bytes for Original String: 11 原始字符串的數據字符串大小(以字節為單位):11
- Data String size in Bytes after SQL TRIM: 9 SQL TRIM之后的數據字符串大小(以字節為單位):9
Let’s look at one more example. This time we want to remove characters App from the leading side. We do not need to make any changes in the script. We can specify the character to remove using SQL TRIM, and it gives the required output.
讓我們再看一個例子。 這次我們要從前端刪除字符App 。 我們不需要在腳本中進行任何更改。 我們可以使用SQL TRIM指定要刪除的字符,并提供所需的輸出。
DECLARE @String VARCHAR(24)= 'Application'; SELECT @String as OriginalString, TRIM('APP' from @String) AS StringAfterTRIM, DATALENGTH(@String) AS 'DataLength String (Bytes)', DATALENGTH( TRIM('APP' from @String) ) AS 'DataLength String (Bytes) After TRIM';
- Data String size in Bytes for Original String: 11 原始字符串的數據字符串大小(以字節為單位):11
- Data String size in Bytes after SQL TRIM: 8 SQL TRIM之后的數據字符串大小(以字節為單位):8
In previous examples, we removed character either from the leading end or trailing end. If we have a character on both sides, it removes them as well.
在前面的示例中,我們從前端或后端刪除了字符。 如果我們兩邊都有角色,它也會將其刪除。
In the following example, we want to remove character A that is on both sides of the string. Execute the script, and it removes the specified characters from both sides.
在下面的示例中,我們要刪除字符串兩側的字符A。 執行該腳本,然后從兩側刪除指定的字符。
DECLARE @String VARCHAR(24)= 'ApplicationA'; SELECT @String as OriginalString, TRIM('A' from @String) AS StringAfterTRIM, DATALENGTH(@String) AS 'DataLength String (Bytes)', DATALENGTH( TRIM('A' from @String) ) AS 'DataLength String (Bytes) After TRIM';
- Data String size in Bytes for Original String: 12 原始字符串的數據字符串大小(以字節為單位):12
- Data String size in Bytes after SQL TRIM: 10 SQL TRIM之后的數據字符串大小(以字節為單位):10
Suppose we have specified a character that is not present on the string. In the following example, we want to remove character A and C from leading or trailing end. We do not have character C on either side, so it does not remove it. It only removes character A and gives the output.
假設我們指定了字符串中不存在的字符。 在下面的示例中,我們要從開頭或結尾刪除字符A和C。 我們兩邊都沒有字符C,因此不會將其刪除。 它僅刪除字符A并提供輸出。
DECLARE @String VARCHAR(24)= 'ApplicationA'; SELECT @String as OriginalString, TRIM('AC' from @String) AS StringAfterTRIM, DATALENGTH(@String) AS 'DataLength String (Bytes)', DATALENGTH( TRIM('AC' from @String) ) AS 'DataLength String (Bytes) After TRIM';- Data String size in Bytes for Original String: 12 原始字符串的數據字符串大小(以字節為單位):12
- Data String size in Bytes after SQL TRIM: 10 SQL TRIM之后的數據字符串大小(以字節為單位):10
TRIM用于表中的記錄 (TRIM for records in a table)
In the previous article, we used a variable to define a string and perform SQL TRIM operation on it. Usually, we require using TRIM on table records. Let’s use it in the following example.
在上一篇文章中,我們使用了變量來定義字符串并對其執行SQL TRIM操作。 通常,我們要求在表記錄上使用TRIM。 在下面的示例中使用它。
In the following examples, it eliminated character R from the string at the leading and trailing end.
在以下示例中,它從字符串的開頭和結尾刪除了字符R。
We can use SQL TRIM function in Update statement as well. Suppose we want to update all records in Employee table and want to eliminate characters Manager from JobTitile.
我們也可以在Update語句中使用SQL TRIM函數。 假設我們要更新Employee表中的所有記錄,并要從JobTitile中刪除字符Manager 。
UPDATE [AdventureWorks2017].[HumanResources].[Employee] SET JobTitle = TRIM ('Manager' from 'Research and Development Manager');It does not treat the Manager as a single word. Trim function checks for each character in the string and eliminates it.
它不會將Manager視為一個單詞。 修剪功能檢查字符串中的每個字符并將其消除。
We can use comma (,) to separate each character as well.
我們也可以使用逗號(,)分隔每個字符。
UPDATE [AdventureWorks2017].[HumanResources].[Employee] SET JobTitle??= TRIM('M,a,n,a,g,e,r' from 'Research and Development Manager');It also does the same work and removes specific characters from the string at a leading and trailing end.
它也做同樣的工作,并從前端和尾端刪除字符串中的特定字符。
結論 (Conclusion)
In this article, we explored the SQL TRIM function with SQL Server 2017 and above. We also learned about the TRIM functions LTRIM and RTRIM available on or before SQL Server 2016 as well.
在本文中,我們使用SQL Server 2017及更高版本探索了SQL TRIM函數。 我們還了解了SQL Server 2016或之前可用的TRIM函數LTRIM和RTRIM。
Depending on the version of SQL Server you are using, you should understand these functions and be familiar with the concepts. I hope you find this article useful. Feel free to provide feedback or ask questions in the comments below.
根據所使用SQL Server版本,您應該了解這些功能并熟悉這些概念。 希望本文對您有所幫助。 歡迎在下面的評論中提供反饋或提出問題。
翻譯自: https://www.sqlshack.com/sql-trim-function/
sql trim函數
總結
以上是生活随笔為你收集整理的sql trim函数_SQL TRIM函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是图床,如何理解图床
- 下一篇: recv/send堵塞和非堵塞