Professional Documents
Culture Documents
Mysql Notescgpt
Mysql Notescgpt
Example 1:
sqlCopy code
SELECT TRIM ( ' Hello ' ) AS trimmed_string;
Output:
diffCopy code
trimmed_string -------------- Hello
In this example, the input string is ' Hello '. The TRIM() function removes the leading
and trailing spaces from the string and returns 'Hello' as the trimmed string.
Example 2:
sqlCopy code
SELECT TRIM ( '***Hello***' , '*' ) AS trimmed_string;
Output:
diffCopy code
trimmed_string -------------- Hello
In this example, the input string is 'Hello'. The TRIM() function removes the asterisks
(*) from both ends of the string and returns 'Hello' as the trimmed string.
Example 3:
sqlCopy code
SELECT TRIM ( '!##!Hello!##!' , '!#' ) AS trimmed_string;
Output:
diffCopy code
trimmed_string -------------- Hello
In this example, the input string is '!##!Hello!##!'. The TRIM() function removes the
exclamation mark (!) and hash (#) characters from both ends of the string and returns
'Hello' as the trimmed string.
The TRIM() function is useful for removing unwanted spaces or characters from
strings. It can be used to clean up data before performing comparisons or further
processing in SQL queries.
Example 1:
sqlCopy code
SELECT SUBSTRING_INDEX( 'Hello, World, How, Are, You' , ', ' , 3 ) AS substring;
Output:
markdownCopy code
substring ------------ Hello, World, How
In this example, the SUBSTRING_INDEX() function extracts the substring before the
third occurrence of the ', ' delimiter. It returns 'Hello, World, How' as the extracted
substring.
Example 2:
sqlCopy code
SELECT SUBSTRING_INDEX( '1|2|3|4|5' , '|' , -2 ) AS substring;
Output:
markdownCopy code
substring -------- 4|5
Here, the function extracts the substring after the second occurrence of the '|'
delimiter, starting from the end of the string. It returns '4|5' as the extracted
substring.
Example 3:
sqlCopy code
SELECT SUBSTRING_INDEX( 'Apples,Oranges,Bananas,Grapes' , ',' , 1 ) AS substring;
Output:
markdownCopy code
substring -------- Apples
In this example, the function extracts the substring before the first occurrence of the
',' delimiter. It returns 'Apples' as the extracted substring.
These examples demonstrate how the position or occurrence of the delimiter affects
the extraction of substrings using the SUBSTRING_INDEX() function.