This document describes 14 string functions in SQL: UPPER(), LOWER(), LTRIM(), RTRIM(), TRIM(), LENGTH(), LEFT(), RIGHT(), INSTR(), INITCAP(), CONCAT(), SUBSTR(), POSITION(), and CHAR(). For each function, it provides the syntax, a description of what the function does, and an example of how to use the function in a SQL query.
UPPER() / UCASE()
Upper()Function Convert a string Small
to upper-case:
Syntax Example
SELECT UCASE(Col_Name)
FROM table_name;
SELECT UCASE(Name)
FROM Employees1;
Lower() / LCASE()
Lower()Function Convert a string upper
to Small-case:
Syntax Example
SELECT LCASE(Col_Name)
FROM table_name;
SELECT LCASE(Name)
FROM Employees1;
LTRIM()
The LTRIM() functionremoves leading
spaces from a string
Syntax Example
SELECT LTRIM(Col_Name)
FROM table_name;
SELECT LTRIM(Name)
FROM Employees1;
RTRIM()
The RTRIM() functionremoves trailing
spaces from a string.
Syntax Example
SELECT RTRIM(Col_Name)
FROM table_name;
SELECT RTRIM(Name)
FROM Employees1;
TRIM()
The TRIM() functionremoves leading and
trailing spaces from a string.
Syntax Example
SELECT TRIM(Col_Name)
FROM table_name;
SELECT TRIM(Name)
FROM Employees1;
LENGTH()
The LENGTH() functionreturns the length of a
string (in bytes).
Syntax Example
SELECT Length(Col_Name)
FROM table_name;
SELECT Length(Name)
FROM Employees1;
LEFT(string, Number ofChar)
The LEFT() function extracts a number of
characters from a string (starting from
left).
Syntax Example
SELECT LEFT(Col_Name , No_of_Char)
FROM table_name;
SELECT LEFT (Name,3)
FROM Employees1;
RIGHT(string, Number ofChar)
The RIGHT() function extracts a
number of characters from a string
(starting from right).
Syntax Example
SELECT RIGHT(Col_Name ,
Number of Char)
FROM table_name;
SELECT RIGHT(Name,3)
FROM Employees1;
INSTR()
The INSTR() functionreturns the position of the first
occurrence of a string in another string.
Syntax Example
SELECT INSTR(string1, string2)
FROM table_name;
SELECT INSTR( NAME, ” i ”)
FROM Employees1;
INITCAP()
Syntax Example
SELECT INITCAP(string); SELECT INITCAP (“dhirendra chauhan”);
The INITCAP() function sets the first
letter of each word in uppercase, all other
letters in lowercase.
CONCAT(String1, String2)
The CONCAT()function adds two or
more expressions together.
Syntax Example
SELECT CONCAT(String1,
String2) FROM table_name;
SELECT CONCAT(Name , City)
FROM Employees1;
Syntax
SELECT SUBSTR/SUBSTRING/MID(string, start,length) FROM table_name;
Example
SELECT SUBSTR(NAME, 3, 4) FROM Employees1;
SELECT SUBSTRING(NAME, 3, 4) FROM Employees1;
SELECT MID(NAME, 3, 4) FROM Employees1;
POSITION(substring IN string)
ThePOSITION() function returns the position of the first occurrence
of a substring in a string.
If the substring is not found within the original string, this function
returns 0.
Syntax Example
SELECT POSITION(substring IN string)
FROM table_name;
SELECT POSITION(“a” IN Name)
FROM Employees1;