This is a very simple task to solve using traditional functions. Here are two examples for two common SQL dialects, but for sure you can adapt to just about any version and language.
The secret is in the function that locates a string within the other, and returns the position numerically:
First, we run this select to get the string up to -
:
UPDATE cartas SET nome = IIF(
CHARINDEX( nome, '-' ) = 0,
nome,
LEFT( nome, CHARINDEX( nome, '-' ) )
);
Now, Number 11: Big Eye GAV-EN090
is only Number 11: Big Eye GAV-
.
Next, we use the second query to just pick up the space before GAV-
:
UPDATE cartas SET nome = IIF(
CHARINDEX( REVERSE(nome), ' ' ) = 0,
"",
REVERSE( SUBSTRING( REVERSE(nome), CHARINDEX( REVERSE(nome), ' ' ) + 1 ) )
);
We had 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 two values;
-
CHARINDEX(palheiro, agulha)
returns the numeric position of agulha
within palheiro
;
-
LEFT(string, quantidade)
returns quantidade
of initial characters of string
;
-
REVERSE(string)
inverts the string, transforming abc
into bca
.
-
SUBSTRING(string,inicio,quantidade)
returns the number of characters in the string from the start position.