SQL Server TRIM character

前端 未结 17 2153
感情败类
感情败类 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:32

    Trim with many cases

    --id = 100 101 102 103 104 105 106 107 108 109 110 111
    select right(id,2)+1  from ordertbl -- 1 2 3 4 5 6 7 8 9 10 11  -- last     two positions are taken
    
    select LEFT('BOB', LEN('BOB')-1) -- BO
    
    select LEFT('BOB*',1) --B
    select LEFT('BOB*',2) --BO
    
    0 讨论(0)
  • 2020-12-09 17:36

    Another pretty good way to implement Oracle's TRIM char FROM string in MS SQL Server is the following:

    • First, you need to identify a char that will never be used in your string, for example ~
    • You replace all spaces with that character
    • You replace the character * you want to trim with a space
    • You LTrim + RTrim the obtained string
    • You replace back all spaces with the trimmed character *
    • You replace back all never-used characters with a space

    For example:

    REPLACE(REPLACE(LTrim(RTrim(REPLACE(REPLACE(string,' ','~'),'*',' '))),' ','*'),'~',' ')
    
    0 讨论(0)
  • 2020-12-09 17:36

    How about.. (in this case to trim off trailing comma or period)

    For a variable:

    -- Trim commas and full stops from end of City
    WHILE RIGHT(@CITY, 1) IN (',', '.'))    
        SET @CITY = LEFT(@CITY, LEN(@CITY)-1)  
    

    For table values:

    -- Trim commas and full stops from end of City
    WHILE EXISTS (SELECT 1 FROM [sap_out_address] WHERE RIGHT([CITY], 1) IN (',', '.'))     
        UPDATE [sap_out_address]    
        SET [CITY] = LEFT([CITY], LEN([CITY])-1)  
        WHERE RIGHT([CITY], 1) IN (',', '.') 
    
    0 讨论(0)
  • 2020-12-09 17:37

    RRIM() LTRIM() only remove spaces try http://msdn.microsoft.com/en-us/library/ms186862.aspx

    Basically just replace the * with empty space

    REPLACE('TextWithCharacterToReplace','CharacterToReplace','CharacterToReplaceWith')

    So you want

    REPLACE ('BOB*','*','')

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

    Solution for one char parameter:

    rtrim('0000100','0') -> select left('0000100',len(rtrim(replace('0000100','0',' '))))

    ltrim('0000100','0') -> select right('0000100',len(replace(ltrim(replace('0000100','0',' ')),' ','.')))

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

    I really like Teejay's answer, and almost stopped there. It's clever, but I got the "almost too clever" feeling, as, somehow, your string at some point will actually have a ~ (or whatever) in it on purpose. So that's not defensive enough for me to put into production.

    I like Chris' too, but the PATINDEX call seems like overkill.

    Though it's probably a micro-optimization, here's one without PATINDEX:

    CREATE FUNCTION dbo.TRIMMIT(@stringToTrim NVARCHAR(MAX), @charToTrim NCHAR(1))
    RETURNS NVARCHAR(MAX)
    AS
    BEGIN
        DECLARE @retVal NVARCHAR(MAX)
    
        SET @retVal = @stringToTrim
    
        WHILE 1 = charindex(@charToTrim, reverse(@retVal))
            SET @retVal = SUBSTRING(@retVal,0,LEN(@retVal))
    
        WHILE 1 = charindex(@charToTrim, @retVal)
            SET @retVal = SUBSTRING(@retVal,2,LEN(@retVal))
    
        RETURN @retVal
    END
    
    --select dbo.TRIMMIT('\\trim\asdfds\\\', '\')
    --trim\asdfds
    

    Returning a MAX nvarchar bugs me a little, but that's the most flexible way to do this..

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