Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Strings In MySQL
Use the powerful string processing toolkit in MySQL for common string manipulation tasks.

| The Length Of Your Hands |

One of the most basic things you can do with a string is find out how long it is. MySQL comes with a handy LENGTH() function that does just that. Take a look:


mysql> SELECT LENGTH("advertisements");
+--------------------------+
| LENGTH("advertisements") |
+--------------------------+
|                       14 |
+--------------------------+
1 row in set (0.00 sec)


String values can easily be concatenated with the CONCAT() function, which accepts a list of the fields to be concatenated. Here's a simple example:


mysql> SELECT CONCAT("Neo","Anderson");
+--------------------------+
| CONCAT("Neo","Anderson") |
+--------------------------+
| NeoAnderson              |
+--------------------------+
1 row in set (0.00 sec)


Want a space between the concatenated values? Fret not, MySQL also has the CONCAT_WS() function, which allows you to specify a separator between the concatenated values. Here's a quick demo:


mysql> SELECT CONCAT_WS(" ","Neo","""The One""","Anderson");
+-----------------------------------------------+
| CONCAT_WS(" ","Neo","""The One""","Anderson") |
+-----------------------------------------------+
| Neo "The One" Anderson              |
+-----------------------------------------------+
1 row in set (0.00 sec)


In this case, the first parameter represents the separator to be used.

How about a more useful real-life example for this function? Consider the following example that fetches the "first_name" and "last_name" fields from the "users" table, and forms a "full_name", suitable for use in your Web page:


mysql> SELECT CONCAT_WS(" ", first_name, last_name) AS 'full_name' FROM  users
WHER username="boba";
+---------------+
| full_name     |
+---------------+
| Boba Fett     |
+---------------+
1 row in set (0.00 sec)


Neat, huh?

Talking about spaces, programmers often forget to remove leading and trailing spaces around the values to be stored in the database, especially in the world of Web-based applications. In such situations, the spaces are stored in the database, leading to much heartburn when it comes to displaying them. If you've ever had this problem, there's an easy solution - MySQL provides a set of handy "trimming" functions, as shown below:


mysql> SELECT LTRIM("       molly dook"), RTRIM("right handed    ");
+----------------------------+----------------------------+
| LTRIM("       molly dook") | RTRIM("right hander     ") |
+----------------------------+----------------------------+
| molly dook                 | right handed               |
+----------------------------+----------------------------+
1 row in set (0.00 sec)


As seen above, the LTRIM() and RTRIM() function can be used to remove leading and trailing spaces respectively. Oh yeah - in case you were wondering what on earth a "molly dook" was, it's Australian for "left-handed person".

If you happen to be ambidextrous, no sweat - the TRIM() function removes both leading and trailing spaces, as shown below:


mysql> SELECT TRIM("   look ma no hands!    ");
+------------------------------------+
| TRIM("   look ma no hands!    ")   |
+------------------------------------+
| look ma no hands!                  |
+------------------------------------+
1 row in set (0.00 sec)


But the best part is yet to come. You see, you can also add keywords to the TRIM() function to remove leading and/or trailing occurrences of a pre-defined character (instead of traditional whitespace). Take a look:


mysql> SELECT TRIM(LEADING "_" FROM "_Neo_");
+--------------------------------+
| TRIM(LEADING "_" FROM "_Neo_") |
+--------------------------------+
| Neo_                           |
+--------------------------------+
1 row in set (0.02 sec)

mysql> SELECT TRIM(TRAILING "_" FROM "_Neo_");
+---------------------------------+
| TRIM(TRAILING "_" FROM "_Neo_") |
+---------------------------------+
| _Neo                            |
+---------------------------------+
1 row in set (0.00 sec)


Feeling paranoid? Remove all the underscore characters with the BOTH keyword:


mysql> SELECT TRIM(BOTH "_" FROM "_Neo_");
+-----------------------------+
| TRIM(BOTH "_" FROM "_Neo_") |
+-----------------------------+
| Neo                         |
+-----------------------------+
1 row in set (0.00 sec)



How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback