|
Custom Search
| |
|
MySQL POSITION() function with examples POSITION() function finds the location of the first occurrence of the substring in a given string. It returns an integer number counting from left to right in the given string. If the substring is not found within the given string, POSITION() function returns 0. LOCATE() function is a synonyms of POSITION() function. Syntax POSITION(substr IN str) Parameter Values
POSITION() function returns 0 if substr is not in str. Returns NULL if substr or str is NULL. Examples 1. The query below finds the position of the first occurrence of the dash character "-" within the given string 011-56984589. It returns integer number 4 which counts from left to right in the given string. -- Result: 4 select position("-" in "011-56984589"); 2. The query below shows that there are two dash characters in the given string 011-56984589-22. It returns integer number 4 which is the position of the first occurrence counting from left to right in the given string. -- Result: 4 select position("-" in "011-56984589-22"); 3. The query below finds the position of the first occurrence of the string "012" within the given string 011-56984589. It returns integer number 0 as the substring does not exist in the given string. -- Result: 0 select position("012" in "011-56984589"); 4. In the query below, we want to extract substring "011" which is the string before the first occurrence of the dash character "-". POSITION function here is used to get the position of the first "-" character, then SUBSTR function is used to extract substring "011" from "011-56984589". -- Result: 011 select substr("011-56984589", 1, position("-" in "011-56984589")-1); 5. In the query below, we want to extract substring "56984589" which is the string after the first occurrence of the dash character "-". POSITION function here is used to get the position of the first "-" character, then SUBSTR function is used to extract substring "56984589" from "011-56984589". -- Result: 56984589 select substr("011-56984589", position("-" in "011-56984589")+1);
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 |