Character-manipulation functions are used to change, extract, alter the character string.
One or more than one characters and words should be passed into the function, and then the function will perform its operation on those words.
In SQL, character-manipulation functions are functions that allow you to perform operations on character or string data. These functions are useful for tasks such as extracting substrings, converting case, and concatenating strings. Here are some common character-manipulation functions in SQL:
- CONCAT(): This function is used to concatenate two or more strings into a single string.
SELECT CONCAT('Hello', ' ', 'World') AS Result;
-- Result: Hello World
- SUBSTRING() or SUBSTR(): This function is used to extract a portion of a string.
SELECT SUBSTRING('Hello World', 1, 5) AS Result;
-- Result: Hello
- UPPER() and LOWER(): These functions convert all characters in a string to uppercase or lowercase, respectively.
SELECT UPPER('hello') AS Result1, LOWER('WORLD') AS Result2;
-- Result1: HELLO, Result2: world
- LTRIM() and RTRIM(): These functions remove leading or trailing spaces from a string.
SELECT LTRIM(' Hello') AS Result1, RTRIM('World ') AS Result2;
-- Result1: Hello, Result2: World
- LEN() or LENGTH(): These functions return the length (number of characters) of a string.
SELECT LEN('Hello') AS Result1, LENGTH('World') AS Result2;
-- Result1: 5, Result2: 5
- CHARINDEX() or POSITION(): These functions return the starting position of a substring within a string.
SELECT CHARINDEX('l', 'Hello') AS Result1, POSITION('o' IN 'World') AS Result2;
-- Result1: 3, Result2: 5
These are just a few examples of character-manipulation functions in SQL. The specific functions available may vary slightly depending on the SQL database management system you are using (e.g., MySQL, PostgreSQL, SQL Server). Always refer to the documentation of your specific database for the accurate list and syntax of functions.