|
Custom Search
| |
|
MySQL MID() function with examples MID() function returns a substring from a string starting from the specified position. SUBSTRING() function and SUBSTR() function are synonyms of MID() function. There are two main forms of using MID() function. MID(str, pos, len) In this form, len is an optional argument. If it's omitted, MID() returns a substring starting at position pos and returns everything to the end of the string. If len presents, MID() returns a substring of len characters long from string str, starting at position pos. or MID(str FROM pos FOR len) In this form, FOR len is an optional argument. If it's omitted, MID() function returns a substring from string str starting at position pos. If FOR len presents, SUBSTRING returns a substring of len characters long from string str, starting at position pos. Parameter Values
Examples: 1. Query below returns a substring from string GeeksEngine, starting at position 1. It extracts from the first character to the end of the string. -- Result: GeeksEngine select mid("GeeksEngine", 1); -- Result: GeeksEngine select mid("GeeksEngine" from 1); 2. Query below returns a substring from string GeeksEngine, starting at position 6. It extracts all the remaining characters starting from the third character which is E. -- Result: Engine select mid("GeeksEngine", 6); -- Result: Engine select mid("GeeksEngine" from 6); 3. Query below returns a substring from string GeeksEngine, starting at position 0. It returns an empty string because the first position in value str always starts with a positive or negative number. -- Result: empty string select mid("GeeksEngine", 0); -- Result: empty string select mid("GeeksEngine" from 0); 4. Query below returns a substring from string GeeksEngine, starting at position 6 and for 3 characters only. -- Result: Eng select mid("GeeksEngine", 6, 3); -- Result: Eng select mid("GeeksEngine" from 6 for 3); 5. Query below returns a substring from string GeeksEngine, starting at position -4. It returns the last 4 characters gine. In this case, a negative value for pos is used which indicates that the MID() function starts from the end of the string and counts backwards. -- Result: gine select mid("GeeksEngine", -4); -- Result: gine select mid("GeeksEngine" from -4); 6. Query below returns a substring of 3 characters long from string GeeksEngine, starting at position 6 which is obtained by counting from right to left because the pos value is negative 6. -- Result: Eng select mid("GeeksEngine", -6, 3); -- Result: Eng select mid("GeeksEngine" from -6 for 3);
Happy Coding!
Other tutorials in this category 1. Using String Functions, Part 1 |
Copyright © 2024 GeeksEngine.com. All Rights Reserved. This website is hosted by HostGator. No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it. |
Home | Feedback | Terms of Use | Privacy Policy |