Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TRIM RTRIM LTRIM Performance

I am only required to do RTRIM() in some part of query but if i do TRIM() will that affect performance.

Is Trim() Slower/Faster/Exactly same(NOT even has negligible difference) compared to RTRIM() AND LTRIM()?

This is with respect to Oracle 10g ONLY.

But in case of SQL Server 2005, Do we have function / method 'x()' such that it can replace RTRIM(LTRIM(' blah.. blah.. ')) to a single function ?

I simply mean of having "single" function for doing the same functionality what both RTRIM() AND LTRIM() does.

like image 935
Pratik Avatar asked Nov 10 '10 06:11

Pratik


People also ask

What is the difference between TRIM () and Rtrim ()?

TRIM has one advantage over LTRIM and RTRIM — it can remove a single character from both ends of the string. LTRIM and RTRIM operate on only one end of the string.

What does TRIM function in SQL performance?

SQL Server TRIM() Function The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string.

What is trim Ltrim Rtrim?

Returns a Variant (String) containing a copy of a specified string without leading spaces (LTrim), trailing spaces (RTrim), or both leading and trailing spaces (Trim). Syntax. LTrim ( string )

What does Ltrim and Rtrim function do?

In LTrim() function a string will be pass as a parameter and it will return the string with no leading spaces. 2. RTrim() Function : It works same like LTrim() function but it will remove all trailing spaces from a string.


1 Answers

Based on this rough test there is a small difference:

DECLARE
n PLS_INTEGER := DBMS_UTILITY.get_time;
s1 VARCHAR2(32767);
s2 VARCHAR2(32767);
BEGIN
s1 := LPAD('X',15000,' ') || RPAD('X',15000,' ');
FOR i IN 1..1000000 LOOP
  NULL;
END LOOP;
DBMS_OUTPUT.put_line('Baseline: ' || (DBMS_UTILITY.get_time - n));
n := DBMS_UTILITY.get_time;
FOR i IN 1..1000000 LOOP
  s2 := LTRIM(s1);
END LOOP;
DBMS_OUTPUT.put_line('LTRIM: ' || (DBMS_UTILITY.get_time - n));
n := DBMS_UTILITY.get_time;
FOR i IN 1..1000000 LOOP
  s2 := RTRIM(s1);
END LOOP;
DBMS_OUTPUT.put_line('RTRIM: ' || (DBMS_UTILITY.get_time - n));
n := DBMS_UTILITY.get_time;
FOR i IN 1..1000000 LOOP
  s2 := TRIM(s1);
END LOOP;
DBMS_OUTPUT.put_line('TRIM: ' || (DBMS_UTILITY.get_time - n));
END;

The difference amounts to up to 0.000128 hundredth's of a second in the worst case:

Baseline: 0
LTRIM: 113
RTRIM: 103
TRIM: 8
Baseline: 0
LTRIM: 136
RTRIM: 133
TRIM: 8
like image 54
Jeffrey Kemp Avatar answered Nov 12 '22 16:11

Jeffrey Kemp