Solved

SUBSTR function in Oracle

Posted on 2000-02-17
14
3,385 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
  • 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Action link in Union Reports Not Working in OBIEE 11g 1 56
selective queries 7 22
Use of Exception to end a Loop 3 20
use lov values 2 37
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.  …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now