Easy way to IT Job

String Manipulation functions in SQL
Share on your Social Media

String Manipulation functions in SQL

Published On: November 1, 2023

When it comes to data management in relational databases, SQL, particularly the String manipulation functions in SQL, is a valuable tool. It goes beyond basic database queries, offering more effective data manipulation and analysis. Given that a significant amount of data is transmitted in text format, having flexible string manipulation capabilities becomes crucial. Raw data often lacks the desired format, necessitating manipulation to impart qualities that are both informative and valuable. 

The five most important String manipulation functions in SQL are discussed in this article.

String Manipulation functions in SQL:

These String manipulation functions in SQL and string operations are standard tools for all SQL developers. Below are some common SQL string functions used in practice.

CONCAT:

The function CONCAT joins two strings together in a consistent manner. If only one of the strings is NULL, it returns NULL; otherwise, it returns the non-NULL argument. A character, numeric, or time data type can be used in any one of the two expressions that this function accepts.

Syntax: CONCAT(‘String1’, ‘String2’)

Example:

Input1: SELECT CONCAT(‘coffee’ ,’beans’) FROM DUAL;

Output1: coffeebeans

Input2: SELECT CONCAT( NULL ,’Espresso’) FROM DUAL;

Output2: Espresso

SUBSTR

The SUBSTR function retrieves a segment of a string between a specified start and end position. If the length of the substring is not provided, SUBSTR returns all characters from the start position to the end of the string.

Syntax:

SUBSTR(‘String’,start-index,length_of_extracted_string)

Example:

Input1: SELECT SUBSTR(‘Artificial Intelligence’, 12) FROM DUAL;

Output1: Intelligence

Input2: SELECT SUBSTR(‘Artificial Intelligence’, 12, 5) FROM DUAL;

Output2: Intel

TRIM:

The TRIM function removes specific leading or trailing padding characters from a string. It has the ability to trim the string from the beginning, the end, or both. If no specific characters are specified for trimming, any additional spaces at the beginning or end of the string are eliminated automatically.

Syntax: TRIM(Leading|Trailing|Both, trim_character FROM trim_source)

Input1: SELECT TRIM(‘A’ FROM ‘ANALYSIS’) FROM DUAL;

Output1: NALYSIS

Input2: SELECT TRIM(‘ database ‘) FROM DUAL;

Output2: database

REPLACE:

The REPLACE function searches for a given character string and, when found, replaces it with a provided replacement string across all instances in the original string. This function is very useful for quickly and effectively recognizing and changing character patterns inside a single function call.

The REPLACE function eliminates every instance of the character string from the input string if no replacement string is provided. The function returns NULL if neither a matching string nor a replacement string are defined.

Syntax: REPLACE(Text, search_string, replacement_string)

Input1: SELECT REPLACE(‘Technology Advancements’, ‘Advancements’, ‘Innovations’) FROM DUAL;

Output1: Technology Innovations

Input2: SELECT REPLACE(‘applesareawesome’, ‘are’, ‘taste’) FROM DUAL;

Output2: applestastesome

Length

The length of the input string is provided by the LENGTH function. When the input string is NULL, LENGTH returns NULL, not Zero. In addition, LENGTH takes into account any extra spaces at the start, middle, or end of the input string when determining the overall length of the string.

Syntax: LENGTH(Column|Expression)

Input1: SELECT LENGTH(‘Exploring New Horizons’) FROM DUAL;

Output1: 22

Input2: SELECT LENGTH(‘ Completing Challenging Tasks ‘) FROM DUAL;

Output2: 32

Conclusion:

In conclusion, this article has covered five important ways of String manipulation functions in SQL, which are crucial for various tasks with data. Getting good at these techniques will make you better at handling words and sentences in databases. For a more thorough understanding, you might want to join SQL Training in Chennai, where you can learn these skills in detail. It’s a great way to become more confident and skilled in managing data using SQL.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.