SQL SERVER字符串中的空格去除


1.LTRIM 
刪除起始空格后返回字符表達式。

語法 
LTRIM   (   character_expression   )

參數 character_expression 是字符或二進制數據表達式。character_expression   可以是常量、變量或列。character_expression   必須是可以隱性轉換為   varchar   的數據類型。否則,使用   CAST   顯式轉換   character_expression。

返回類型 
varchar

注釋 
兼容級別可能影響返回值。有關兼容級別的更多信息,請參見   sp_dbcmptlevel。  

示例 
下例使用   LTRIM   字符刪除字符變量中的起始空格。

DECLARE   @string_to_trim   varchar(60) 
SET   @string_to_trim   =   '           Five   spaces   are   at   the   beginning   of   this 
      string. ' 
SELECT   'Here   is   the   string   without   the   leading   spaces:   '   +   
      LTRIM(@string_to_trim) 
GO

下面是結果集:

------------------------------------------------------------------------ 
Here   is   the   string   without   the   leading   spaces:   Five   spaces   are   at   the   beginning   of   this   string.                          

(1   row(s)   affected)

2.RTRIM 
截斷所有尾隨空格后返回一個字符串。

語法 
RTRIM   (   character_expression   )   

參數 
character_expression 由字符數據組成的表達式。character_expression   可以是常量、變量,也可以是字符或二進制數據的列。character_expression   必須為可隱性轉換為   varchar   的數據類型。否則請使用   CAST   函數顯式轉換   character_expression。 
 

返回類型 
varchar  

注釋  兼容級別可能影響返回值。有關更多信息,請參見   sp_dbcmptlevel。  


示例 
下例顯示如何使用   RTRIM   刪除字符變量中的尾隨空格。

DECLARE   @string_to_trim   varchar(60) 
SET   @string_to_trim   =   'Four   spaces   are   after   the   period   in   this   sentence.         ' 
SELECT   'Here   is   the   string   without   the   leading   spaces:   '   +   CHAR(13)   + 
      RTRIM(@string_to_trim) 
GO

下面是結果集:

(1   row(s)   affected) 
------------------------------------------------------------------------   
Here   is   the   string   without   the   leading   spaces:   Four   spaces   are   after   the   period   in   this   sentence.                       
(1   row(s)   affected)

你可以RTRIM(LTRIM(字段)) --刪除起始空格,截斷所有尾隨空格后返回字符串

select   ltrim(rtrim(title))   from   table1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM