0

Both the below queries return the same result. Can you please let me know why and which is more appropriate?

select substr('87731000',0,8) from dual;

select substr('87731000',1,8) from dual;

1 Answer 1

4

This version is more appropriate:

select substr('87731000',1,8) from dual;

The second parameter is the start position. In oracle, the start position for a string is always 1.

Both statements work just fine, but that's only because the database is being helpful and assuming that if you enter '0', you mean to start at the beginning of the string.

From the oracle docs: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions162.htm

If position is 0, then it is treated as 1.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot , Joel!
And if you use 0, then when someone else sees your code, and need to write a substr starting at character position 2, they may use 1 thinking it is zero-based!
@PunterVicky don't forget mark the question as resolved!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.