1

I was checking the PostgreSQL Manual the last days and I have noticed that substring() is explained there with substring('string' FROM [int] FOR [int]).

I normally just use substring('string', [int], [int]) because I learned it that way from MySQL my simple question is now if this two have any differences is the first one maybe the better choice to use in PostgreSQL or does do the same and these are just 2 different kinds of doing the same thing?

3
  • 6
    You can use substr(string, int, int) instead if you want. I think the substring(... from ...) syntax stems from the SQL standard (somethign which MySQL does not really care about) Commented Feb 9, 2018 at 11:36
  • 1
    Yes, substring(... from ...) is the ANSI/ISO SQL way. Commented Feb 9, 2018 at 11:46
  • Oke thanks first to both of you so you explained me that substring(...from...) is the normal Syntax while the other one was implemented by mysql makes sense to me even I figured out that mysql is a big difference to all other SQL languages and that's mostly not a good thing! But is there like a speed difference? Or can I just assume that both syntax types just call the same function? Commented Feb 9, 2018 at 12:28

1 Answer 1

3

SUBSTRING(char_exp FROM start_position [FOR length]) syntax is part of SQL-92 standard and you are encouraged to use it.

This syntax also works in MySQL.

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

3 Comments

Oke thanks first. I got that substring(.... from ....) is the standard syntax but how does it make a difference or is substring('String',int,int) like an alias?
No difference, it is just two syntaxes for same function.
The SQL committee love adding words to things, presumably because they think it makes the syntax more human-readable. My favourite is that they then use the same keywords for other purposes, giving the completely nonsensical syntax SUBSTRING('foo' FROM 'pattern' FOR 'escape-character'), which also takes its own special format of regular expression.

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.