当前位置:网站首页>Usage of trim, ltrim and rtrim functions of Oracle

Usage of trim, ltrim and rtrim functions of Oracle

2022-06-22 06:16:00 La la la~~~~~

Basic usage :

  1. Remove spaces before and after the specified string

select trim(' hello caicai ')trim from dual;-- When no alias is added , There is still a space to the right of the display string .

  

2. Remove the space around the specified string

select ltrim(' hello caicai ')ltrim from dual;-- Left

select rtrim(' hello caicai ')rtrim from dual;-- Right

Advanced usage :

Receive other parameters to remove the left of the specified string 、 Characters on the right and both sides .

The advanced usage here can not only remove half width spaces , You can also remove all blank spaces , And basic trim Usage: only half width spaces can be removed .

  1. The case where the interception set is one character ( Be careful : The intercept set here can only be one character , Otherwise, the report will be wrong )

select trim(leading  ' '  from  '   hello caicai  ') leadingtrim from dual;

select trim(trailing  ' '  from  '   hello caicai  ') trailingtrim from dual;

select trim(both ' '  from  '   hello caicai  ') bothtrim from dual;

select trim(leading  'y'  from  'ya   hello caicai  y') leadingtrim from dual;

select trim(trailing  'y'  from  'ya   hello caicai y') trailingtrim from dual;

select trim(both 'y'  from  'ya   hello caicai y') bothtrim from dual;

No keywords leading,trailing,both, Its usage is equivalent to trim Basic usage .

select trim( 'y'  from  'ya   hello caicai  y') leadingtrim from dual;

2. When the interception set is multiple characters :

ltrim(string1,string2),rtrim(string1,string2)

ltrim(string1,string2): From a string string1 The left side starts to remove and string2 A character that matches a single character in a character set , Until string1 Encountered a character in , The character is not in string2 In the character set .

In a nutshell , take string2 Each character in the is associated with string1 Each character starting on the left matches , If the same , Then continue to match , If it is different, return its characters and the remaining characters .

select ltrim('abcbcaxcba','abc') ltrim from dual;

rtrim(string1,string2): From a string string1 The right side starts to remove and string2 A character that matches a single character in a character set , Until string1 Encountered a character in , The character is not in string2 In the character set .

In a nutshell , take string2 Each character in the is associated with string1 Each character starting on the right matches , If the same , Then continue to match , If it is different, return its characters and the remaining characters .

select rtrim('abcbcaxcba','abc') rtrim from dual;

Be careful :

In the use of trim,ltrim,rtrim Advanced use of , The blank space corresponds to Airbus , Empty string corresponds to empty string , Otherwise, you will not get the results you want . Note spaces and empty strings , Space refers to a space in quotation marks , An empty string means that there is nothing between the quotation marks .

Such as :

                        Space               Empty string

SELECT ltrim('  abcbcaxcba','') rtrim FROM dual;

                         Space             Space

SELECT ltrim('  abcbcaxcba',' ') rtrim FROM dual;

If you are interested in this knowledge , Clickable Link for details .

原网站

版权声明
本文为[La la la~~~~~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220611070409.html