mmanju
asked on
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
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
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?
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.
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?
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
''
should be:
' '
Gio
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The binary equivalent is 32, so then
select field1 from xyz
where SUBSTR(field1,4,3) = chr(32)||chr(32)||chr(32)
select field1 from xyz
where SUBSTR(field1,4,3) = chr(32)||chr(32)||chr(32)
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.
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.
You could do SUBSTR(field1,4,3) = ' '
ASKER
The data includes 3 spaces. That means the data should be
100<space><space><space>78 90
200<space><space><space>23 93
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?
100<space><space><space>78
200<space><space><space>23
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?
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.
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?
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.
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.
ASKER
Thanks very much for all your answers. It was of great help.
Bye
Bye