Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select only n number of characters from VARCHAR(MAX) field

I have a field with datatype VARCHAR and MAX length. However, I just want to print only the first 2000 characters from that field. I cannot use VARCHAR(2000) as the datatype for the field upon table creation since there are a records exceeding this length and as you might all know, this results in a 'String or binary data would be truncated' error.

What I'm doing is sending an email report which may get too lengthy because of that field so I just want to output the first 2000 characters.

Any help would be greatly appreciated! Thanks in advance!

like image 571
Smiley Avatar asked Jan 11 '23 17:01

Smiley


1 Answers

you can use substring.

select SUBSTRING(yourcolumnname,0,2000) from yourtablename
like image 135
Neel Avatar answered Jan 23 '23 14:01

Neel