Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate the text returned for a column in a MySQL query

Tags:

sql

mysql

I have a table in a MySQL database that I am running simple SELECT queries on (for quick diagnostics/analysis - since I am not running phpmyadmin on the server - for security reasons).

I would like to be able to truncate the returned data using something like this:

select id, LEFT(full_name, 32), age FROM user 

where user is a table that contains the columns id, full_name and age

I tried the above statement and it didn't work. anyone knows how to do this?

[Edit]

Sorry, when I said it dosen't work, I mean mySQL simply returns the STRING "LEFT(full_name, 32)" as an alias for the column 'full_name' and outputs the field value - which in this case, can be as long as 256 chars.

like image 251
morpheous Avatar asked Jul 20 '10 13:07

morpheous


People also ask

How do you truncate a field value in SQL?

We use the Truncate command to delete the data stored in the table. The working of truncate command is similar to the working of Delete command without a where clause.

Is truncate a text function in MySQL?

MySQL - TRUNCATE() FunctionThis function keeps the specified number of digits after the decimal and removes the remaining.

What is data truncation in MySQL?

TRUNCATE TABLE empties a table completely. It requires the DROP privilege. Logically, TRUNCATE TABLE is similar to a DELETE statement that deletes all rows, or a sequence of DROP TABLE and CREATE TABLE statements. To achieve high performance, TRUNCATE TABLE bypasses the DML method of deleting data.


2 Answers

select id, SUBSTRING(full_name,1, 32), age FROM user 

Quoting mysql.com:

For all forms of SUBSTRING(), the position of the first character in the string from which the substring is to be extracted is reckoned as 1.

like image 174
mvds Avatar answered Oct 02 '22 02:10

mvds


select id, SUBSTRING(full_name,1, 32), age FROM user  
like image 45
Fosco Avatar answered Oct 02 '22 01:10

Fosco