SQL Server TRIM character

前端 未结 17 2155
感情败类
感情败类 2020-12-09 17:11

I have the following string: \'BOB*\', how do I trim the * so it shows up as \'BOB\'

I tried the RTRIM(\'BOB*\',\'*\') but does not work as says needs only 1 paramet

相关标签:
17条回答
  • 2020-12-09 17:43

    If you only want to remove a single '*' character from the value when the value ends with a '*', a simple CASE expression will do that for you:

    SELECT CASE WHEN RIGHT(foo,1) = '*' THEN LEFT(foo,LEN(foo)-1) ELSE foo END AS foo
      FROM (SELECT 'BOB*' AS foo)
    

    To remove all trailing '*' characters, then you'd need a more complex expression, making use of the REVERSE, PATINDEX, LEN and LEFT functions.

    NOTE: Be careful with the REPLACE function, as that will replace all occurrences of the specified character within the string, not just the trailing ones.

    0 讨论(0)
  • 2020-12-09 17:43

    I've used a similar approach to some of the above answers of using pattern matching and reversing the string to find the first non-trimmable character, then cutting that off. The difference is this version does less work than those above, so should be a little more efficient.

    • This creates RTRIM functionality for any specified character.
    • It includes an additional step set @charToFind = case... to escape the chosen character.
    • There is currently an issue if @charToReplace is a right crotchet (]) as there appears to be no way to escape this.

    .

    declare @stringToSearch nvarchar(max) = '****this is****a  ** demo*****'
    , @charToFind nvarchar(5) = '*' 
    
    --escape @charToFind so it doesn't break our pattern matching
    set @charToFind = case @charToFind 
        when ']' then '[]]' --*this does not work / can't find any info on escaping right crotchet*
        when '^' then '\^'
        --when '%' then '%' --doesn't require escaping in this context
        --when '[' then '[' --doesn't require escaping in this context
        --when '_' then '_' --doesn't require escaping in this context
        else @charToFind
    end
    
    select @stringToSearch
    , left
    (
        @stringToSearch
        ,1 
        + len(@stringToSearch)
        - patindex('%[^' + @charToFind  + ']%',reverse(@stringToSearch))
    )
    
    0 讨论(0)
  • 2020-12-09 17:43

    Try this:

    Original

    select replace('BOB*','*','')
    

    Fixed to be an exact replacement

    select replace('BOB*','BOB*','BOB')
    
    0 讨论(0)
  • 2020-12-09 17:45

    If you want to remove all asterisks then it's obvious:

    SELECT REPLACE('Hello*', '*', '')
    

    However, If you have more than one asterisk at the end and multiple throughout, but are only interested in trimming the trailing ones, then I'd use this:

    DECLARE @String VarChar(50) = '**H*i****'
    SELECT LEFT(@String, LEN(REPLACE(@String, '*', ' ')))              --Returns: **H*i
    

    I updated this answer to include show how to remove leading characters:

    SELECT RIGHT(@String, LEN(REPLACE(REVERSE(@String), '*', ' ')))    --Returns: H*i****
    

    LEN() has a "feature" (that looks a lot like a bug) where it does not count trailing spaces.

    0 讨论(0)
  • 2020-12-09 17:48

    An other approach ONLY if you want to remove leading and trailing characters is the use of TRIM function. By default removes white spaces but have te avility of remove other characters if you specify its.

    SELECT TRIM('=' FROM '=SPECIALS=') AS Result;

    Result  
    --------
    SPECIALS
    

    Unfortunately LTRIM and RTRIM does not work in the same way and only removes white spaces instead of specified characters like TRIM does if you specify its.

    Reference and more examples: https://database.guide/how-to-remove-leading-and-trailing-characters-in-sql-server/

    0 讨论(0)
提交回复
热议问题