How to use the INICAP() function in SQL
The SQL INITCAP() function
The INITCAP() function converts the first character of each string to upper case and the remaining characters to lower case.
Syntax
INITCAP(string)
Parameter
string: This represents the string to be altered.
Example
The following code shows how to use the INITCAP() 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','boo-01-345');INSERT INTO ProductVALUES (102,'hand Bag','$65','boo-01-238');INSERT INTO ProductVALUES (103,'ipad','$1200','boo-01-103');INSERT INTO ProductVALUES (104,'cereal','$30','boo-01-775');INSERT INTO ProductVALUES (105,'microwave','$520','boo-01-788');INSERT INTO ProductVALUES (106,'cloth clips','$15','boo-01-924');INSERT INTO ProductVALUES (108,'zara Perfume','$120','boo-01-245');-- QuerySELECT id, product_name, INITCAP(product_name) AS new_product_nameFROM Product;
Explanation
- Lines 1–6: We create a table called
Productthat has theid,product_name,price, andbook_idcolumns. - Lines 9–22: We insert data into the table.
- Lines 25–26: We retrieve the data in the
id, andproduct_namecolumns. We then use theINITCAP()function to alter the first character of each product name to uppercase and store it in a new column,new_product_name.