Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

Ok, so here is the issue.

I have a table with some columns and 'subject' is one of the columns. I need to get the first 10 letters from the 'subject' field no matter the 'subject' field contains a string with 100 letters.

For example,

Table - tbl. Columns - id, subject, value.

SQL Query:

SELECT subject FROM tbl WHERE id ='$id';

The result I am getting is, for example

Hello, this is my subject and how are you

I only require the first 10 characters

Hello, thi

I can understand that I can remove the rest of the characters using php substr() but that's not possible in my case. I need to get the excess characters removed by MySQL. How can this be done?

1 Answer

0 votes
by (40.7k points)

Try using the below code:

SELECT LEFT(subject , 10) FROM tbl 

For more information, you can refer to MySQL Doc. 

Related questions

0 votes
3 answers
0 votes
1 answer
asked Dec 17, 2020 in SQL by Appu (6.1k points)
0 votes
2 answers
0 votes
1 answer
asked Jul 9, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...