Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get first characters from a String

Tags:

mysql

I have a string with 30 characters but i only need the 20 of them. How can i get the first 20?

I tried this substring(ltrim(rtrim(field_value)),20)

But didn't work. Any ideas?

like image 408
Mark Roll Avatar asked Sep 06 '25 09:09

Mark Roll


1 Answers

Edit:

In My SQL, you could use LEFT (or SUBSTRING) to SELECT certain number of characters from a string. Since what you need is the first characters, I suggest to use LEFT:

SELECT LEFT(field_value, 20) FROM MyTableName

Original:

(As the tag was originally C#, the following solution below provides solution for C# problem)

You should use Substring(0, 20). The first argument (0) is the starting index, and the second argument (20) is the length of the string you want to take.

For example:

var str = "123456789012345678901234567890";
str = str.Substring(0, 20);
like image 69
Ian Avatar answered Sep 09 '25 06:09

Ian