Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » Mysql »

consulta de actualización

Estas en el tema de consulta de actualización en el foro de Mysql en Foros del Web. buenos días, tengo que hacer una consulta de actualización, de la siguiente forma, en una tabla tengo un campo1 = numero - texto, para poder ...
  #1 (permalink)  
Antiguo 11/01/2010, 05:01
 
Fecha de Ingreso: julio-2002
Mensajes: 813
Antigüedad: 21 años, 8 meses
Puntos: 2
consulta de actualización

buenos días,

tengo que hacer una consulta de actualización, de la siguiente forma, en una tabla tengo un campo1 = numero - texto, para poder hacer busquedas basadas en texto tengo que pasar esta información a otro campo campoN = texto - numero, no todos los registros tienen valores para ese campo1

como podría hacerlo?

update nombretabla set campoN = campo1 where campo1<>""

hasta aquí mas o menos lo tengo, es decir modifico el valor de campoN con el valor de campo1, pero como se realiza desde sql la transformación del valor de campo1 para que en campoN tenga texto - numero, cuando en campo1 tengo numero - texto,

numero es siempre de 6 dígitos
y campo1 tiene siempre esta estructura: número + espacio + "-" + espacio + texto

un saludo,
josé carlos.
  #2 (permalink)  
Antiguo 11/01/2010, 06:44
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 1 mes
Puntos: 574
Respuesta: consulta de actualización

Código MySQL:
Ver original
  1. update nombretabla set campoN = RIGHT(campo1,LENGTH(campo1)-9);

o

Código MySQL:
Ver original
  1. update nombretabla set campoN = SUBSTRING(campo1,10);

Del manual

Cita:
RIGHT(str,len)

Returns the rightmost len characters from the string str, or NULL if any argument is NULL.

mysql> SELECT RIGHT('foobarbar', 4);
-> 'rbar'

This function is multi-byte safe.
Cita:
LENGTH(str)

Returns the length of the string str, measured in bytes. A multi-byte character counts as multiple bytes. This means that for a string containing five two-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.

mysql> SELECT LENGTH('text');
-> 4
Cita:
SUBSTRING(str,pos), SUBSTRING(str FROM pos), SUBSTRING(str,pos,len), SUBSTRING(str FROM pos FOR len)

The forms without a len argument return a substring from string str starting at position pos. The forms with a len argument return a substring len characters long from string str, starting at position pos. The forms that use FROM are standard SQL syntax. It is also possible to use a negative value for pos. In this case, the beginning of the substring is pos characters from the end of the string, rather than the beginning. A negative value may be used for pos in any of the forms of this function.

For all forms of SUBSTRING(), the position of the first character in the string from which the substring is to be extracted is reckoned as 1.

mysql> SELECT SUBSTRING('Quadratically',5);
-> 'ratically'
mysql> SELECT SUBSTRING('foobarbar' FROM 4);
-> 'barbar'
mysql> SELECT SUBSTRING('Quadratically',5,6);
-> 'ratica'
mysql> SELECT SUBSTRING('Sakila', -3);
-> 'ila'
mysql> SELECT SUBSTRING('Sakila', -5, 3);
-> 'aki'
mysql> SELECT SUBSTRING('Sakila' FROM -4 FOR 2);
-> 'ki'

This function is multi-byte safe.

If len is less than 1, the result is the empty string.
Quim
  #3 (permalink)  
Antiguo 11/01/2010, 06:58
 
Fecha de Ingreso: julio-2002
Mensajes: 813
Antigüedad: 21 años, 8 meses
Puntos: 2
Respuesta: consulta de actualización

gracias.

un saludo,
josé carlos.

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:49.