SQL String Functions – CONCAT, LENGTH, SUBSTRING, TRIM




💻 Introduction

Working with strings is a key part of SQL. Functions like CONCAT, LENGTH, SUBSTRING, and TRIM help manipulate and clean text values efficiently.

1️⃣ SQL CONCAT – Joining Strings

The CONCAT function is used to join two or more strings.

SELECT CONCAT('Hello', ' ', 'World') AS Greeting;
-- Output → Hello World

2️⃣ SQL LENGTH – String Length

The LENGTH (or LEN in some databases) function returns the number of characters in a string.

SELECT LENGTH('Database') AS StringLength;
-- Output → 8

3️⃣ SQL SUBSTRING – Extracting Parts of Strings

The SUBSTRING function extracts a portion of a string starting from a given position.

SELECT SUBSTRING('Hello SQL', 7, 3) AS ExtractedText;
-- Output → SQL

4️⃣ SQL TRIM – Removing Spaces

The TRIM function removes spaces (or specific characters) from the beginning and end of a string.

SELECT TRIM('   SQL   ') AS CleanText;
-- Output → SQL
Tip: Other useful string functions include UPPER(), LOWER(), REPLACE(), LPAD(), and RPAD().

✅ Key Takeaways

  • CONCAT → Join multiple strings
  • LENGTH → Find string length
  • SUBSTRING → Extract substring
  • TRIM → Remove spaces/specified characters
  • Useful for cleaning and formatting text data

📘 Quick Reference – Common SQL String Functions

Function Description Example Result
CONCAT(s1, s2) Joins strings together CONCAT('SQL', ' Functions') SQL Functions
LENGTH(str) Returns string length LENGTH('Hello') 5
SUBSTRING(str, start, len) Extracts part of a string SUBSTRING('Database', 5, 3) bas
TRIM(str) Removes spaces from both ends TRIM(' SQL ') SQL
UPPER(str) Converts text to uppercase UPPER('sql') SQL
LOWER(str) Converts text to lowercase LOWER('SQL') sql
REPLACE(str, from, to) Replaces substring REPLACE('SQL Tutorial','SQL','DB') DB Tutorial

📚 Continue Learning

💬 Got a question about SQL String Functions? Drop it in the comments below and let’s discuss!




Leave a Comment