Supongamos que tenemos una tabla llamada tabla_1 (original ehh) en una base de datos Postgres cuyos campos y registros son los siguientes:
id name ----- ----------------------------------------------- 1 BBVA atiende solo hasta las 17:00 hrs 2 Banco de Chile atiende solo hasta las 17:00 hrs 3 Banco Estado atiende hasta las 15:00 hrs 3 record(s) selected [Fetch MetaData: 0/ms] [Fetch Data: 0/ms] [Executed: 05-02-09 11:50:03 AM CLST ] [Execution: 0/ms]
Si les pidieran cambiar el horario de atención para que diga “14:00 hrs” en vez de “17:00 hrs” ¿cómo lo harían? teniendo en cuenta que deben actualizar sólo las filas involucradas en la restricción y que deben conservar el resto de contenido que posee el campo.
Solución:
Mediante el uso combinado de OVERLAY POSITION podemos hacer un UPDATE de la tabla con el requerimiento solicitado.
| Function | Return Type | Description | Example | Result |
|---|---|---|---|---|
| string || string | text | String concatenation | 'Post' || 'greSQL' | PostgreSQL |
| string || non-string or non-string || string | text | String concatenation with one non-string input | 'Value: ' || 42 | Value: 42 |
bit_length(string) |
int | Number of bits in string | bit_length('jose') | 32 |
char_length(string) or character_length(string) |
int | Number of characters in string | char_length('jose') | 4 |
lower(string) |
text | Convert string to lower case | lower('TOM') | tom |
octet_length(string) |
int | Number of bytes in string | octet_length('jose') | 4 |
overlay(string placing string from int [for int]) |
text | Replace substring | overlay('Txxxxas' placing 'hom' from 2 for 4) | Thomas |
position(substring in string) |
int | Location of specified substring | position('om' in 'Thomas') | 3 |
substring(string [from int] [for int]) |
text | Extract substring | substring('Thomas' from 2 for 3) | hom |
substring(string from pattern) |
text | Extract substring matching POSIX regular expression. See Section 9.7 for more information on pattern matching. | substring('Thomas' from '...$') | mas |
substring(string from pattern for escape) |
text | Extract substring matching SQL regular expression. See Section 9.7 for more information on pattern matching. | substring('Thomas' from '%#"o_a#"_' for '#') | oma |
trim([leading | trailing | both] [characters] from string) |
text | Remove the longest string containing only the characters (a space by default) from the start/end/both ends of the string | trim(both 'x' from 'xTomxx') | Tom |
upper(string) |
text | Convert string to uppercase | upper('tom') | TOM |
Script para Solución:
UPDATE tabla_1
SET name = OVERLAY(name PLACING '14:00' FROM POSITION('17:00' IN name) FOR 5)
WHERE POSITION('17:00' IN name) > 0
Si quieren previsualizar como se vería el cambio, pueden hacer un SELECT antes de ejecutar el UPDATE y revisar como sería la salida con los cambios realizados.
SELECT OVERLAY(name PLACING '14:00' FROM POSITION('17:00' in name) FOR 5)
FROM tabla_1
WHERE POSITION('17:00' IN name) > 0
SALIDA:
overlay
-----------------------------------------------
Banco de Chile atiende solo hasta las 14:00 hrs
BBVA atiende solo hasta las 14:00 hrs
2 record(s) selected [Fetch MetaData: 0/ms] [Fetch Data: 0/ms]
[Executed: 05-02-09 11:50:36 AM CLST ] [Execution: 0/ms]
Entonces después de ejecutar el update nuestra tabla contendría lo siguiente:
id name ----- ----------------------------------------------- 1 BBVA atiende solo hasta las 14:00 hrs 2 Banco de Chile atiende solo hasta las 14:00 hrs 3 Banco Estado atiende hasta las 15:00 hrs 3 record(s) selected [Fetch MetaData: 0/ms] [Fetch Data: 0/ms] [Executed: 05-02-09 11:52:44 AM CLST ] [Execution: 0/ms]
Publicado por Pps en Febrero 5, 2009 at 11:07 am
tnis tpo ah!!!!…..oye y pq no aprovecha y m desbloquea el fcbk !!! xD!!!
pd.: exigo mis vacas !
Publicado por Pps en Febrero 5, 2009 at 11:07 am
ahahah,,,,se m olvidaba…..wen dato…=)