Tags
MySQL , TRIM
Asked 2 years ago
31 May 2022
Views 783
Mitul Dabhi

Mitul Dabhi posted

how to use function TRIM in MySQL?

how to use TRIM in MySQL ? can you please explain function TRIM in MySQL with some example?
denyy

denyy
answered May 31 '22 00:00

TRIM() function in MySQL is used to remove leading and trailing space/characters from data.

Syntax :

TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str)

Parameter : TRIM() function in MySQL use parameter as following:

BOTH | LEADING | TRAILING :
BOTH or LEADING or TRAILING any one can be used in TRIM() function.
if BOTH option is set than it tell the TRIM() function to remove both leading and trailing unwanted characters from a string .
if LEADING option is set than it tell the TRIM() function to remove leading unwanted characters from a string .
if TRAILING option is set than it tell the TRIM() function to remove trailing unwanted characters from a string .
the TRIM() function uses the BOTH as default option.

removed_str : It is a string which we want to remove. If not given, spaces will be removed.

str : It identifies the string from which we want to remove removed_str.

Returns : It returns a string that has unwanted characters removed.


 SELECT TRIM(TRAILING  FROM " DATA ")

returns : DATA


 SELECT TRIM(LEADING FROM " DATA ")

returns : DATA


 SELECT TRIM(TRAILING  "A" FROM " DATA")

returns : DAT


 SELECT TRIM(LEADING "D" FROM " DATA")

returns : ATA


 SELECT TRIM(LEADING "d" FROM "DATA")

returns : DATA
TRIM function have case insensitive replace the string


shabi

shabi
answered May 31 '22 00:00

The TRIM function is used to trim a given string in MySQL.
The TRIM function remove white space from the start and end of the given string in MySQL.
for example " an example " string have white space before and after and if you want to remove it in MySQL The TRIM function will do the job.


select TRIM(' an example ')  


above query returns "an example"(without whitespace of start and end of string)
Post Answer