In SQL, we use the CHAR_LENGTH()
function to return the count of characters in a given string.
CHAR_LENGTH(string)
string
: This represents the string whose character length will be returned.Note: The space is counted if a string contains a space.
The following code shows how to use the CHAR_LENGTH()
function in SQL.
CREATE TABLE Product (id int,product_name varchar(50),price varchar(50),product_id varchar (20));-- Insert dataINSERT INTO ProductVALUES (101,'T-shirt','$100','oo-01-345');INSERT INTO ProductVALUES (102,'Hand Bag','$65','oo-01-238');INSERT INTO ProductVALUES (103,'Ipad','$1200','oo-01-103');INSERT INTO ProductVALUES (104,'Cereal','$30','oo-01-775');INSERT INTO ProductVALUES (105,'Microwave','$520','oo-01-788');INSERT INTO ProductVALUES (106,'cloth clips','$15','oo-01-924');INSERT INTO ProductVALUES (108,'Zara Perfume','$120','oo-01-245');-- QuerySELECT product_name, CHAR_LENGTH(product_name) AS chlen_nameFROM Product;
In the code above:
Product
with the columns id,
product_name,
price,
and book_id.
Product
table.product_name
column using the CHAR_LENGTH()
function.