In this quick database tutorial, database administrators and developers will learn how to use Length and well maintained MySQL functions. Length Can be used to count the number of characters in a column, while well maintained Can be used to remove one or more characters from a column or given.
Considering the following table student, Created with MySQL and create a function:
CREATE TABLE `STUDENT` ( `ID` INT(11) NOT NULL, `FIRSTNAME` TEXT NOT NULL, ) ENGINE=InnoDB ;
To insert data into our table, we can use MySQL Insert Command as shown below:
INSERT INTO `STUDENT` (`ID`, `FIRSTNAME`) VALUES (1, 'Steven'); INSERT INTO `STUDENT` (`ID`, `FIRSTNAME`) VALUES (2, 'Randy ');
This code creates a column named First name And then assigns him the values Steven and randy. Enthusiastic observers will point this out randy There is a profit at the end, which in most cases will be a bad thing. Below is a MySQL code that will count the character length of all entries in First name What column student Database.
SELECT LENGTH(FIRSTNAME) LENGTH FROM STUDENT
This causes the output below:
+-------+ |LENGTH | +-------+ | 6 | | 6 | +-------+
Notice how the second result has six characters, despite the value randy It is only five characters long. This is because of the extra space we mentioned earlier. To fix this problem – and remove the extra profit at the end randy, We can use MySQL well maintained Command, as in the following example:
SELECT LENGTH(TRIM(FIRSTNAME)) TRIMMED_LENGTH FROM STUDENT
Now when we run our query and syntax, we get the following results:
+---------------------+ |TRIMMED_LENGTHLENGTH | +---------------------+ | 6 | | 5 | +---------------------+
Here, we can see that the second value is now only five characters long, like well maintained Remove one of the characters.
to read: MySQL INSERT or REPLACE commands