DISQUS

Eddie Awad’s Blog: SQL Function Spotlight: TRIM

  • Laurent Schneider · 1 year ago
    there is one major advantage of using ltrim (rtrim), with ltrim you can skip any character from a range, with trim the character is a single character !
    <pre>
    SQL> select ltrim('elcaroracle','racle') from dual

    LTRIM(
    ------
    oracle
    </pre>
  • Eddie Awad · 1 year ago
    Indeed, if you want to trim more than one character on either or both ends of a string, TRIM is not the function to use.
  • Mathias Magnusson · 1 year ago
    I like using trim on dates. I have seen many elaborate ways of converting to char to remove it and then convert back to date. Using trim seems to be the slick way to just remove the time (making it zero).
  • Eddie Awad · 1 year ago
    Hmm! What is the difference between trim(sysdate) and to_char(sysdate)?
  • Brian Tkatch · 1 year ago
    So, TRIM and TO_CHAR seem the same by a DATE:

    SELECT TRIM(SysDate), DUMP(TRIM(SysDate)) FROM Dual UNION ALL
    SELECT TO_CHAR(SysDate), DUMP(TO_CHAR(SysDate)) FROM Dual;

    05-SEP-08 Typ=1 Len=9: 48,53,45,83,69,80,45,48,56
    05-SEP-08 Typ=1 Len=9: 48,53,45,83,69,80,45,48,56

    TRUNC, however, leaves it as a DATE, which is possible the best way to remove the time.

    SELECT SysDate, DUMP(SysDate) FROM Dual UNION ALL
    SELECT TRUNC(SysDate), DUMP(TRUNC(SysDate)) FROM Dual;

    05-SEP-08 Typ=13 Len=8: 216,7,9,5,8,51,44,0
    05-SEP-08 Typ=13 Len=8: 216,7,9,5,0,0,0,0
  • Eddie Awad · 1 year ago
    Thanks Brian. By the way, nice use of the DUMP function.