SUBSTR function in Oracle

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
mmanjuAsked:
Who is Participating?
 
bkowalskiConnect With a Mentor Commented:
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
 
bkowalskiCommented:
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
 
bkowalskiCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
bkowalskiCommented:
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
 
jaramillCommented:
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
 
bkowalskiCommented:
The binary equivalent is 32, so then

select field1 from xyz
where SUBSTR(field1,4,3) = chr(32)||chr(32)||chr(32)
0
 
mshaikhCommented:
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
 
bkowalskiCommented:
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
 
mshaikhCommented:
You could do SUBSTR(field1,4,3) = '   '
0
 
mmanjuAuthor Commented:
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
 
bkowalskiCommented:
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
 
bkowalskiCommented:
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
 
mshaikhCommented:
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
 
mmanjuAuthor Commented:
Thanks very much for all your answers. It was of great help.

Bye
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.