PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > mysql.general > finding count of spaces in a string
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
finding count of spaces in a string

Réponse
 
LinkBack Outils de la discussion
Vieux 04/09/2007, 16h36   #1
dpgirago@mdanderson.org
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut finding count of spaces in a string

We have numerous identical tables with a varchar column that holds data
like this: "0 0 0 1 0 1 0 25 7 0 139 0 9". Essentially there are a bunch
of integers with a single space as a separator. There _should_ be no more
than 30 entries ( and 29 spaces ), but sometimes the system misfires and
there are more or less. Is there a MySQL solution to getting a count of
the spaces present in the field, figuring that spaces + 1 will equal
entries? It's fairly straight forward using a PHP application, but I'd
like to get the DB server to accomplish this task. Not having much luck
finding a solution in the manual.

Thanks,

David
  Réponse avec citation
Vieux 04/09/2007, 16h47   #2
Baron Schwartz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding count of spaces in a string

Hi,

dpgirago@mdanderson.org wrote:
> We have numerous identical tables with a varchar column that holds data
> like this: "0 0 0 1 0 1 0 25 7 0 139 0 9". Essentially there are a bunch
> of integers with a single space as a separator. There _should_ be no more
> than 30 entries ( and 29 spaces ), but sometimes the system misfires and
> there are more or less. Is there a MySQL solution to getting a count of
> the spaces present in the field, figuring that spaces + 1 will equal
> entries? It's fairly straight forward using a PHP application, but I'd
> like to get the DB server to accomplish this task. Not having much luck
> finding a solution in the manual.


You can find the number of spaces like this:

select length(col) - length(replace(col, ' ', ''))

Baron
  Réponse avec citation
Vieux 04/09/2007, 16h50   #3
Jay Pipes
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding count of spaces in a string

dpgirago@mdanderson.org wrote:
> We have numerous identical tables with a varchar column that holds data
> like this: "0 0 0 1 0 1 0 25 7 0 139 0 9". Essentially there are a bunch
> of integers with a single space as a separator. There _should_ be no more
> than 30 entries ( and 29 spaces ), but sometimes the system misfires and
> there are more or less. Is there a MySQL solution to getting a count of
> the spaces present in the field, figuring that spaces + 1 will equal
> entries? It's fairly straight forward using a PHP application, but I'd
> like to get the DB server to accomplish this task. Not having much luck
> finding a solution in the manual.


SELECT
CHAR_LENGTH(field_name) - CHAR_LENGTH(REPLACE(field_name, ' ', '')) as
num_spaces FROM my_table;

Cheers,

Jay
  Réponse avec citation
Vieux 04/09/2007, 16h50   #4
Rolando Edwards
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding count of spaces in a string

Try this:

mysql> SELECT LENGTH('0 0 0 1 0 1 0 25 7 0 139 0 9') - LENGTH(REPLACE('0 0 0 1 0 1 0 25 7 0 139 0 9',' ','')) + 1;
+-----------------------------------------------------------------------------------------------------+
| LENGTH('0 0 0 1 0 1 0 25 7 0 139 0 9') - LENGTH(REPLACE('0 0 0 1 0 1 0 25 7 0 139 0 9',' ','')) + 1 |
+-----------------------------------------------------------------------------------------------------+
| 13 |
+-----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

or Try it like this:

mysql> SELECT LENGTH(st) - LENGTH(REPLACE(st,' ','')) + 1 FieldCount FROM (SELECT '0 0 0 1 0 1 0 25 7 0 139 0 9' st) A;
+------------+
| FieldCount |
+------------+
| 13 |
+------------+
1 row in set (0.00 sec)

----- Original Message -----
From: dpgirago@mdanderson.org
To: mysql@lists.mysql.com
Sent: Tuesday, September 4, 2007 11:36:41 AM (GMT-0500) America/New_York
Subject: finding count of spaces in a string

We have numerous identical tables with a varchar column that holds data
like this: "0 0 0 1 0 1 0 25 7 0 139 0 9". Essentially there are a bunch
of integers with a single space as a separator. There _should_ be no more
than 30 entries ( and 29 spaces ), but sometimes the system misfires and
there are more or less. Is there a MySQL solution to getting a count of
the spaces present in the field, figuring that spaces + 1 will equal
entries? It's fairly straight forward using a PHP application, but I'd
like to get the DB server to accomplish this task. Not having much luck
finding a solution in the manual.

Thanks,

David

--
Rolando A. Edwards
MySQL DBA

SWMX, Inc.
1 Bridge Street
Irvington, NY 10533
(914) 406-8406 (Main)
(201) 660-3221 (Mobile)

  Réponse avec citation
Vieux 04/09/2007, 16h55   #5
Francesco Riosa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: finding count of spaces in a string

dpgirago@mdanderson.org ha scritto:
> We have numerous identical tables with a varchar column that holds data
> like this: "0 0 0 1 0 1 0 25 7 0 139 0 9". Essentially there are a bunch
> of integers with a single space as a separator. There _should_ be no more
> than 30 entries ( and 29 spaces ), but sometimes the system misfires and
> there are more or less. Is there a MySQL solution to getting a count of
> the spaces present in the field, figuring that spaces + 1 will equal
> entries? It's fairly straight forward using a PHP application, but I'd
> like to get the DB server to accomplish this task. Not having much luck
> finding a solution in the manual.
>
>


SELECT 1
+ CHAR_LENGTH("0 0 0 1 0 1 0 25 7 0 139 0 9")
- CHAR_LENGTH(REPLACE("0 0 0 1 0 1 0 25 7 0 139 0 9", " ", ""))
AS ret


HopeIt,
Francesco

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h54.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10346 seconds with 13 queries