1

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?

Mark Roll
  • 506
  • 3
  • 6
  • 15
  • What did it output? if it didn't work... Also Ian has the right idea and that may be your answer – Brendon May 20 '16 at 09:36
  • When you say "it didn't work" what exactly do you mean. The SubString function in .Net will do the job you need. – Pheonyx May 20 '16 at 09:37
  • My dummy fault was that i forgot to check from what character will begin to read. – Mark Roll May 20 '16 at 10:24
  • Seems like your problem is actually for `MySQL` though you originally tag it as `C#`. Is this true? Or you actually are asking problem for `C#`? – Ian May 21 '16 at 14:14

3 Answers3

9

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);
Ian
  • 30,182
  • 19
  • 69
  • 107
  • 2
    A yes, i am a little fool here. The fact is that i concerned about trim() function and forgot the starting character. Thank you again! – Mark Roll May 20 '16 at 09:37
  • 2
    @Marios Ah yes, sometimes we can miss what is obvious because of distraction. Not a problem. :) – Ian May 20 '16 at 09:38
0

You can use SubString method to get first 20 characters out of your string.

Try this way:

 str= str.Substring(0, 20);
error_handler
  • 1,191
  • 11
  • 19
-1

Try This

SELECT SUBSTR(STR,POS,LEN)

Luna
  • 16
  • 4