dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL
Expertise: Intermediate
Dec 26, 2018

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Figure Out the Length of Text in a Column in MySQL

Considering the following table STOCKPRICE and its details.

CREATE TABLE `STOCKPRICE` (
`ID` INT(11) NOT NULL,
`NAME` TEXT NOT NULL COLLATE 'utf8_bin',
`PRICE` INT(11) NOT NULL
)
COLLATE='utf8_bin'
ENGINE=InnoDB
;

Also, use the following to create sample data as below.

INSERT INTO `STOCKPRICE` (`ID`, `NAME`, `PRICE`) VALUES (1, 'ABC Ltd', 15);
INSERT INTO `STOCKPRICE` (`ID`, `NAME`, `PRICE`) VALUES (2, 'XYZ Ltd ', 19);
INSERT INTO `STOCKPRICE` (`ID`, `NAME`, `PRICE`) VALUES (3, 'AAA Ltd', 16);

Now, you can find out the length of the text in the NAME column:

SELECT LENGTH(NAME) LENGTH FROM STOCKPRICE, which will result in the following values

+ -- -- -- -+
|LENGTH |
+ -- -- -- -+
| 7 |
| 8 |
| 7 |
+ -- -- -- -+

Also, you can use the TRIM function to remove the extra spaces. SELECT LENGTH(TRIM(NAME)) LENGTH FROM STOCKPRICE, which will result in the following values. Please note the change in the value for row 2:

+ -- -- -- -+
|LENGTH |
+ -- -- -- -+
| 7 |
| 7 |
| 7 |
+ -- -- -- -+
Sridhar M S
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Thanks for your registration, follow us on our social networks to keep up-to-date