how to add one space before and at end of a field in table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

how to add one space before and at end of a field in table

Post by mayanktalwar1988 »

is there any possible query to add just one space both at the start and at the end in one of field of a table..i want to do for all the rows in a particular field...anyone has a query for that in sql then plz share.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: how to add one space before and at end of a field in ta

Post by AbraCadaver »

I can't think of any possible reason for doing this in the database, but if you're using mysql:

[text]UPDATE `table_name` SET `field_name` = CONCAT(' ', field_name, ' ')[/text]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: how to add one space before and at end of a field in ta

Post by mayanktalwar1988 »

its a workaround to make some query run faster..thanks
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: how to add one space before and at end of a field in ta

Post by mayanktalwar1988 »

hey also tell me the opposite one for backup ....removing the added spaces.. opposite for above query :D
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: how to add one space before and at end of a field in ta

Post by AbraCadaver »

For leading and trailing spaces use TRIM(). For other characters you'll use some string function to return just the part that you want: http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: how to add one space before and at end of a field in ta

Post by mayanktalwar1988 »

ok thanks your query worked
Post Reply