Hello, you are not logged in.  Login or sign up
Toad on Twitter Follow Toad Search Toad World Search
Blogger List   

All Recent Blog Entries
 

Johannes Ahrends
Unicode and Toad

Ben Boise
Toad SC Discussions

Kevin Dalton
Benchmark Factory

Steven Feuerstein
Oracle PL/SQL

Devin Gallagher
Toad SC discussions

Stuart Hodgins
JProbe Discussions

  Henrik "Mauritz" Johnson
Toad Tips & Tricks on the "other" Toads
  Mark Kurtz
Toad SC discussions
  Michael Lumbard
Toad SC discussions
Daniel Norwood
Toad for Data Analysts,
Toad Extension for Visual Studio
Debbie Peabody
Toad for Data Analysts
Gary Piper
Toad Reports Manager
John Pocknell
Toad for Oracle, JProbe
Kuljit Sangha
Toad SC discussions
Bert Scalzo Indicates Oracle ACE status
Toad for Oracle, Data Modeling, Benchmarking
Jeff Smith
Toad product family
Richard To
SQL Optimization
Jim Wankowski
DB2 - LUW and z/OS
John Weathington
  Toad World Editor
Toad World issues

  Toad Data Modeler Opens in a new window
Data Modeling
 
  Real Automated Code Testing for Oracle
Quest Code Tester blog

Blogs
Toad and Database Commentaries

Toad World blogs are a mix of insightful how-tos from Quest experts as well as their commentary on experiences with new database technologies.  Have some views of your own to share?  Post your comments!  Note:  Comments are restricted to registered Toad World users.

Do you have a topic that you'd like discussed?  We'd love to hear from you.  Send us your idea for a blog topic.

Resolving an LTRIM mystery
 
Location: Blogs Steven Feuerstein's Blog    
 StevenFeuersteinTW Thursday, October 15, 2009 6:42 AM

I received this email today:

Hi Steven

When executing the following query I receive an output with the first letter missing. Is this an Oracle bug?

select LTRIM('<br />believe it or not','<br />') from dual;

Thanks, Marcel

First of all, I need to point out to Marcel and perhaps many other PL/SQL developers: you do not need to call LTRIM (or SYSDATE or USER or....) within a SELECT from dual. Instead you can run this SQL function directly inside PL/SQL, as in:

BEGIN
   DBMS_OUTPUT.put_line (LTRIM ('<br />believe it or not', '<br />'));
END;

If you do, you will see this output:

elieve it or not

Marcel feels that this is the wrong result, but in fact it is perfectly correct. You see, LTRIM does not trim by patterns of characters. It trims any and all of the individual characters you provide in the list. So what Marcel requested in the above code can be translated into English as follows:

"Please trim < and b and r and / and > from the beginning of this string."

Thus, the following block will display the same results:

BEGIN
   DBMS_OUTPUT.put_line (
      LTRIM (
'<<>>brrrrrrr/////<br />believe it or not', '<br />'));
END;

I assume what Marcel wanted to do was remove
from the start of the string only if it is present. To do that, you might write something like this:

CREATE OR REPLACE FUNCTION lremove (string_in         IN VARCHAR2
                                  , remove_in         IN VARCHAR2
                                  , case_sensitive_in IN BOOLEAN DEFAULT TRUE
                                   )
   RETURN
VARCHAR2
IS
BEGIN
   RETURN CASE CASE
                  WHEN
case_sensitive_in THEN INSTR (string_in, remove_in)
                  ELSE INSTR (UPPER (string_in), UPPER (remove_in))
               END
             WHEN
1
             THEN
                SUBSTR (
string_in, LENGTH (remove_in) + 1)
             ELSE
                string_in
          END;
END lremove;
/
BEGIN
   DBMS_OUTPUT.put_line (lremove ('<br />believe it or not', '<br />'));
   DBMS_OUTPUT.put_line (lremove ('Whether you believe it or not', '
'
));
END;
/
believe it or not
Whether you believe it or not

 

Permalink |  Trackback

Comments (3)  
By sstuber on Wednesday, November 18, 2009 12:58 PM
I don't understand, the examples above, why is "b" going to be trimmed of the beginning of the string if it's not in the trim-set?


SQL> SELECT LTRIM('
2 believe it or not', '
3 ') FROM DUAL;

LTRIM('BELIEVEITO
-----------------
believe it or not

SQL>
SQL> BEGIN
2 DBMS_OUTPUT.put_line(LTRIM('
3 believe it or not', '
4 '));
5 END;
6 /
believe it or not

PL/SQL procedure successfully completed.

SQL>

By DanCJones on Tuesday, December 15, 2009 7:44 AM
It took me a few minutes to work this out, but this example has been formatted by the browser.

The example should be:
select LTRIM('[br/]believe it or not','[br/]') from dual;

only with angle brackets instead of square brackets - but when you put that in HTML it turns the [br/] bits into carriage returns.

By StevenFeuersteinTW on Tuesday, December 15, 2009 8:07 AM
Yes, you are right, Dan. I am sorry I did not deal with this sooner. I will see if I can get the text fixed so that this will not be a problem.

Search Blog Entries
 
Copyright 2010 by Quest Software  | Terms Of Use | Privacy Statement | Contact Us