This is a very simple task to solve using traditional functions. Here are two examples for two common dialects of SQL, but surely you can adapt to virtually any version and language.
The secret lies in the function that locates a string within the other, and returns the position numerically:
First, we run this select to get the string to the -
:
UPDATE cartas SET nome = IIF(
CHARINDEX( nome, '-' ) = 0,
nome,
LEFT( nome, CHARINDEX( nome, '-' ) )
);
Now, Number 11: Big Eye GAV-EN090
was left only Number 11: Big Eye GAV-
.
Then we use the second query to only take up the space before the GAV-
:
UPDATE cartas SET nome = IIF(
CHARINDEX( REVERSE(nome), ' ' ) = 0,
"",
REVERSE( SUBSTRING( REVERSE(nome), CHARINDEX( REVERSE(nome), ' ' ) + 1 ) )
);
We had it made Number 11: Big Eye GAV-
and now we have Number 11: Big Eye
!
In T-SQL use CHARINDEX()
, IIF()
and SUBSTRING()
, in Mysql use INSTR()
, IF()
and SUBSTR()
Functions used:
IIF(condicao,seVerdadeiro,seFalso)
depending on the condition returns one of the two values;
CHARINDEX(palheiro, agulha)
returns the numeric position of the agulha
within the palheiro
;
LEFT(string, quantidade)
returns to quantidade
of initial characters of string
;
REVERSE(string)
reverses the string, transforming abc
in bca
.
SUBSTRING(string,inicio,quantidade)
returns the number of characters in the string from the start position.
Have you tried using regular expressions? In Postgresql you have the function regexp_replace and you will find the explanations in: http://www.postgresql.org/docs/current/interactive/functions-matching.html#FUNCTIONS-POSIX-REGEXP
– user4552
I ended up using the expressions in excel I removed and upei the bank again, but it was this way even, as postgresql never messed with it, anyway worth
– Arsom Nolasco
@Arsomnolasco saw that you solved with regex, but anyway updated the answer according to the statement, using SQL.
– Bacco