Nerd Alert: Trim trailing LF/CR characters in Oracle

Lots of Nerd Alerts lately, but I haven’t unloaded the camera to see which family pics turned out to be in focus…

PROBLEM: Your data has zero or more trailing commas followed by a non-printing character, typically LF/CR.
SOLUTION: regexp_replace!

regexp_replace(a.SOME_COL,',*[^[:print:]]$','') as SOME_COL

The regular expression breaks up like so:

',*[^[:print:]]$'

  1. ,* means look for zero or more commas, followed by…
  2. [ ] …the contents of the brackets, which breaks up as
    • ^ means not the things in the inner brackets
    • [:print:] is the class of printable characters
  3. $ …leading to the end of the line, which in regexp speech is “$”
This entry was posted in computers/programming. Bookmark the permalink.

2 Responses to Nerd Alert: Trim trailing LF/CR characters in Oracle

  1. Hub's says:

    It’s not works.
    But this is works :

    select regexp_replace(chr(10)||’ ‘||chr(9)||chr(13)||’text’||chr(10)||’ ‘||chr(9)||chr(13),’^[[:space:]]+|[[:space:]]+$’,”) as SOME_COL
    from dual

    Regards.

    • bturnip says:

      I don’t have an Oracle box to test the expression above. It looks to me that the expression in comment is solving for a different problem than what I outlined, but if it helps somebody else who comes across this post, all the better.

Leave a Reply

Your email address will not be published. Required fields are marked *