In this article, we will discuss the character functions in SQL. A function that accepts several characters or numbers called parameters and generates a character value is called a character function or string function.
The string functions return a string value; as a result set, they also contain many features. SQL offers the following character data types:
- LONG RAW
With the help of a comprehensive range of character functions provided by SQL, you may learn more about strings and change their contents in various ways. The two categories of character functions are as follows:
Case-Manipulative: LOWER, UPPER, and INITCAP
Character-Manipulative: CONCAT, LENGTH, SUBSTR, INSTR, LPAD, RPAD, TRIM, and REPLACE
Table of Contents
Character Functions in SQL
Lowercase alpha character values get converted using this function. If the input string is of a fixed length, that length of a string will be returned by LOWER. Since the case is unimportant for numerals and special characters like the dollar symbol ($) or modulus (%), LOWER will not affect any characters within the string which are not letters.
SELECT LOWER('ALPHABET') FROM DUAL;
This function raises the case of alpha character values. If the entering string is fixed-length, the UPPER function will also return a fixed-length string. Since the case is unimportant for numerals and special characters like the dollar symbol ($) or modulus (%), UPPER will not alter any characters within the string which are not letters.
SELECT UPPER('alphabet') FROM DUAL;
The initial letter of every word is converted to uppercase using this function, while the remaining letters get changed to lowercase. You can separate the string’s words with a #, _, or space.
SELECT INITCAP('sharukh is watching television') FROM DUAL;
Output: Sharukh Is Watching Television
String2 is always added (concatenated) to the final segment of string1 using this function. Suppose any of the given strings are NULL, the function outputs input parameters that are not NULL. CONCAT returns NULL if either or both strings are NULL.
Syntax: CONCAT(‘str1’ , ‘str2’)
SELECT CONCAT('social,' 'studies') FROM DUAL;
The total length of the supplied string gets rebounded by this function. The LENGTH function outputs NULL and not Zero if the given input string is NULL. Additionally, the LENGTH function contains the extra spaces and returns the entire length of a string if the given input string has additional spaces around the beginning, middle, or end of the string.
SELECT LENGTH('sharukh is watching telivision') FROM DUAL;
This function returns the string’s starting and ending characters. When a substring length isn’t specified, SUBSTR returns all characters up to the string’s end, i.e., from the starting position specified.
Syntax: SUBSTR(‘str’ , start-index , length_of_extracted_string)
SELECT SUBSTR('sharukh is watching television', 8) FROM DUAL;
Output: is watching television
This function returns the character or string’s numerical location within the specified string. You can also select a starting point of m and a string’s occurrence number, n. Additionally, if no starting point is determined, the search is automatically initiated from index 1. If there is no match after searching the string, the INSTR function produces 0.
Syntax: INSTR(‘str1’ , ‘str2’)
SELECT INSTR('Shahrukh is watching television, 'is') AS Found;
LPAD() & RPAD():
These functions return strings that have been left or right-padded (depending on the use), giving rise to the “L” in “LPAD” and the “R” in “RPAD,” respectively. There is also length and pad string given. The input string contains padding on the left or right (depending on the purpose) with spaces if a padded string is not specified.
Syntax: LPAD(str , len , padstr)
SELECT LPAD('computer', 12, '*') AS LeftPaddedString;
Syntax: RPAD(str , len , padstr)
SELECT RPAD('computer', 12, '*') AS RightPaddedString;
The above function trims the supplied string from the beginning or end. (or both). If there is additional space at the beginning or end of a string and no string or character is inclined to get removed, those different characters get removed.
Syntax: TRIM( [characters from] str)
SELECT TRIM('c' FROM 'computer') FROM DUAL;
This function looks through a character string alone; if detected, it replaces it at all instances with a provided replacement string. The REPLACE function helps find character patterns and alter all occurrences of those patterns at once.
The REPLACE function deletes every instance of that particular character string within the input string if a substitute string is not supplied. REPLACE returns NULL if neither an acceptable nor replacement string is provided.
Syntax: REPLACE(text, search_str, replacement_str)
SELECT REPLACE('political science,' 'political,' 'computer') FROM DUAL;
Output: computer science
What is the LEN function in SQL?
The LEN function in SQL is a character function that returns the length of a string in characters. You can use it with any character data type.
What is the LEFT function in SQL?
The LEFT function in SQL is a character function that returns a specified number of characters from the beginning of a string.
What is the RIGHT function in SQL?
The RIGHT function in SQL is a character function that returns a specified number of characters from the end of a string.
What is the SUBSTRING function in SQL?
The SUBSTRING function in SQL is a character function that returns a portion of a string. You can specify the starting position and length of the substring.
What is the UPPER function in SQL?
The UPPER function in SQL is a character function that converts all characters in a string to uppercase.
What is the LOWER function in SQL?
The LOWER function in SQL is a character function that converts all characters in a string to lowercase.
So this is all about character functions in SQL. This article discusses the types of character functions. We hope this solves your query.
See also, Difference between SQL and T-SQL.
Aloukik Rathore is an Entrepreneur, Writer, Marketer, Musician, and Co-founder of Cannibals Media, a modern-day digital marketing company. He started blogging at the tender age of 16 and has been in the digital marketing industry ever since.