39
SELECT SUBSTRING(FieldName,0,20) FROM Table

I try on phpmyadmin and returned empty value. How i use this function ?

2
  • 2
    Documentation for SUBSTRING dev.mysql.com/doc/refman/5.0/en/… Commented Dec 5, 2011 at 14:21
  • would you pick the most upvoted answer as the answer ? Commented Jul 26, 2016 at 14:22

4 Answers 4

60

The SUBSTR start index is 1 in mysql. So, you should change your code to:

SELECT SUBSTR(FieldName,1,20) FROM TABLE;

Notice that SUBSTR() is a synonym for SUBSTRING() so they can be used interchangeably.

You should also checkout the documentation for SUBSTRING()

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

2 Comments

I like how now in 2025, AI was saying to start with ZERO, and gave me fits... then I find the correct answer here, by a human, provided 14 years ago. Thanks!
AI can be nice sometimes, but at the same time I think people are giving it to much credits at this point. Remember once at work when my coworker let AI generate code for a fetch query; we needed to debug it for a long time since it did not work. One of the parameters used an underscore instead e.g. countCode became country_Code, jesus christ =)
16

you must use SELECT SUBSTRING(FieldName,1,20) FROM Table, because from 0 is 0 :)

use 1

ups, someone wrote answer faster than I :)

Comments

11

Try SELECT SUBSTRING(FieldName,1,20) FROM Table

Comments

-1

Use the syntax like my code below, I think it will be useful for your situation

SELECT  
SUBSTRING(course_designater,6,3) as 'Course number'                   
FROM    Courses
WHERE   course_designater LIKE 'Excel%' 
LIMIT 10

Comments

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.