Trim Whitespaces (New Line and Tab space) in a String in Oracle

前端 未结 14 1032
挽巷
挽巷 2021-01-30 10:29

I need to trim New Line (Chr(13) and Chr(10) and Tab space from the beginning and end of a String) in an Oracle query. I learnt that there is no easy way to trim multiple charac

14条回答
  •  独厮守ぢ
    2021-01-30 11:27

    I know this is not a strict answer for this question, but I've been working in several scenarios where you need to transform text data following these rules:

    1. No spaces or ctrl chars at the beginning of the string
    2. No spaces or ctrl chars at the end of the string
    3. Multiple ocurrencies of spaces or ctrl chars will be replaced to a single space

    Code below follow the rules detailed above:

    WITH test_view AS (
      SELECT CHR(9) || 'Q   qwer' || CHR(9) || CHR(10) ||
             CHR(13) || ' qwerqwer     qwerty  ' || CHR(9) || 
             CHR(10) || CHR(13) str
      FROM DUAL
    ) SELECT 
         str original
        ,TRIM(REGEXP_REPLACE(str, '([[:space:]]{2,}|[[:cntrl:]])', ' ')) fixed
      FROM test_view;
    
    
    ORIGINAL               FIXED                 
    ---------------------- ----------------------
        Q   qwer           Q qwer qwerqwer qwerty
    
     qwerqwer     qwerty                                         
    
    1 row selected.
    

提交回复
热议问题