Solved

SUBSTR function in Oracle

Posted on 2000-02-17
14
3,425 Views
Last Modified: 2012-08-14
Given the following data set from a table xyz with a field name field1 :

100   7890
200   2393

and a sequel :

SELECT field1
FROM xyz
WHERE SUBSTR(field1,4,3)=' '

Normally I would expect to see both the
rows returned. But, It appears like we need to include in the quotes the length of the string that we have used, as in this example '   ' as opposed to ' ', which is what one would use when a SUBSTR is not used.

Is there any reason why SUBSTR behaves like this?

Any information would be appreciated.

Thank you
0
Comment
Question by:mmanju
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
  • 2
  • +1
14 Comments
 
LVL 3

Expert Comment

by:bkowalski
ID: 2532081
What is inside your quotes, a space or is there nothing (null)?  And what is returned from your select statement?  Also I assume field1 is the data '7890' and '2393' and the first field is a key value, is that right?
0
 
LVL 3

Expert Comment

by:bkowalski
ID: 2532097
Without answers to my questions, I think your select statement will not return any rows because SUBSTR starts at the fourth position and looks for 3 characters.  Your data ends at the fourth position, so it will return '0' for '7890' and '3' for '2393', neither of which will evaluate to true when compared to ' ' or null.
0
 
LVL 3

Expert Comment

by:bkowalski
ID: 2532103
Sorry, I see now how I've misinterpreted the question.  The data is one field like '100   7890' and '200   2393'.  How many spaces in there, 3?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:jaramill
ID: 2532107
Yes, we need to know what 100, and 200 mean.  And what result do you get when you run the query.  But you definitely need a space inside the single quotes, not both of them together:
''
should be:

' '

Gio
0
 
LVL 3

Accepted Solution

by:
bkowalski earned 100 total points
ID: 2532116
This select should work:

select field1 from xyz
where SUBSTR(field1,4,3) = chr(n)||chr(n)||chr(n)

where n is the binary equivalent of a space.  I can't remember what it is, but you should be able to look it up easily.
0
 
LVL 3

Expert Comment

by:bkowalski
ID: 2532133
The binary equivalent is 32, so then

select field1 from xyz
where SUBSTR(field1,4,3) = chr(32)||chr(32)||chr(32)
0
 
LVL 6

Expert Comment

by:mshaikh
ID: 2532135
SUBSTR(field1,4,3) will return three spaces '   '  and this is being compared to a single space '  '. So this test will fail and you won't get these rows returned.
0
 
LVL 3

Expert Comment

by:bkowalski
ID: 2532143
BTW, the clearest way to code this is to use chr(32)||chr(32)||chr(32) instead of '   ' where there are 3 spaces inside the quotes.  It is more readable to those maintaining code.
0
 
LVL 6

Expert Comment

by:mshaikh
ID: 2532149
You could do SUBSTR(field1,4,3) = '   '
0
 

Author Comment

by:mmanju
ID: 2532169
The data includes 3 spaces. That means the data should be

100<space><space><space>7890
200<space><space><space>2393

and in the sequel, the where condition :

WHERE SUBSTR(field1,4,3) = ' '
[ Its only one space in the quote. As bkowalski suggested, if we use '   ', which is 3 spaces in the quote then it works )

My question is, why should we use these 3 spaces as opposed to just one space. That kind of query would work had I not used SUBSTR function.

Any thoughts?
0
 
LVL 3

Expert Comment

by:bkowalski
ID: 2532182
You have coded your substring to return 3 spaces.  SUBSTR(field1,4,3) means to start at char 4 in field1 and return 3 characters.  So you have to compare to 3 spaces then.
0
 
LVL 3

Expert Comment

by:bkowalski
ID: 2532187
I think you may not understand what SUBSTR does.  Maybe you want to reduce your 3 spaces (or maybe an unknown number of spaces) to just one space?
0
 
LVL 6

Expert Comment

by:mshaikh
ID: 2532299
The substr(field1,4,3) function returns an exact  three characters which is then compared to ' '. And they are not equal.

But, you are right if you do '   '=' ' is works because the SQL parser interprets multiple space to be a single space so, '   '=' ' becomes ' '=' ' after parsing. But, the  substr(field1,4,3) is applied after parsing as it retrives field1 values from table. At this point the parsing has already been done and the comparision is literally between three space and single space.
0
 

Author Comment

by:mmanju
ID: 2532405
Thanks very much for all your answers. It was of great help.

Bye
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question