SQL Server TRIM character

前端 未结 17 2175
感情败类
感情败类 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.

提交回复
热议问题