3
I have this code that’s working perfectly, but I need random values to appear instead of the X. I want the returned result to be composed of random letters and the position of upper and lower case letters to be obeyed according to the user’s input into the function at the time of the tests. I use this function to "shuffle" people’s names. The first name is preserved and I want the rest of the name to appear with random characters.
Example:
- Entree:
Pedro Souza
- Upshot
Pedro Fscet
The number of letters must remain the same and must be case sensitive.
Does anyone know how I can do this? I’m using SQL DEVELOPER.
create or replace FUNCTION EMBARALHA_NOME (NOME IN VARCHAR2) RETURN VARCHAR2 AS
primeiro_Nome VARCHAR2(100);
embaralha VARCHAR2(100);
nome_Cortado VARCHAR2(100);
nome_Embaralhado VARCHAR2(100);
BEGIN
if (NOME is NULL) then
nome_Embaralhado := NULL;
else
primeiro_Nome := NVL(SUBSTR(NOME, 0, INSTR(NOME, ' ')-1), NOME);
nome_Cortado := LTRIM(NOME, primeiro_Nome);
embaralha := REGEXP_REPLACE(nome_Cortado, '[A-Za-z]', 'x');
nome_Embaralhado := CONCAT(primeiro_Nome, embaralha);
end if;
return nome_Embaralhado;
END EMBARALHA_NOME;
But this way I won’t be able to define the size of the result according to the size of the name, right? And it won’t even follow the order of the uppercase and lowercase letters, correct?
– Lucas Vinícius
@Lucasvinícius You can use
LENGTH(NOME)
as the whole value. To ensure a first letter uppercase and the rest lowercase, just invoke the random generation function twice: one with format'U'
and size 1, the other with format'L'
and sizeLENGTH(NOME) - 1
.– Oralista de Sistemas
What about the declaration of this function twice? Declares everything at once on the same line?
– Lucas Vinícius
And if you have a name like that: Pedro Sousa F da Silva
– Lucas Vinícius