Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Select first 30 chars in a sql query?

SELECT First 20 Chars of(ColName) from DB

Is this possible?

like image 421
Will Avatar asked Oct 12 '10 13:10

Will


5 Answers

SELECT left(ColName,20) AS First20 /*(Or 30 if we are looking at the title)*/
FROM YourTable
like image 71
Martin Smith Avatar answered Oct 19 '22 08:10

Martin Smith


SUBSTRING(ColName, 1, 30)
like image 33
reko_t Avatar answered Oct 19 '22 06:10

reko_t


SELECT CONVERT(VARCHAR(30), ColName) from DB
like image 33
Gabe Avatar answered Oct 19 '22 08:10

Gabe


Assuming that colname is VARCHAR, all the above will pad shorter strings to 20 characters.

If this is not what you want, then:

SELECT RTRIM(LEFT(colname, 20)) FROM DB

like image 25
smirkingman Avatar answered Oct 19 '22 06:10

smirkingman


You can simply use one of the built in string functions. There are many variants so its best to see which one suits your situation best.

Enjoy!

like image 1
Doug Avatar answered Oct 19 '22 07:10

Doug