Solved

# SUBSTR function in Oracle

Posted on 2000-02-17
3,372 Views
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
Question by:mmanju
• 8
• 3
• 2
• +1

LVL 3

Expert Comment

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

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

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

LVL 3

Expert Comment

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

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

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

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

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

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

Author Comment

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

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

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

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

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

Bye
0

## Featured Post

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and theâ€¦
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  â€¦
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
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.