7
I have a scenario that I need to sort my records where:
The child inherited the surname of the father and/or his mother?
Note in the image that if one of the child’s surnames exists in the parent’s name the field PAI?
gets true. Similarly to the field MAE?
.
Scenario building
CREATE TABLE USUARIOS
(CODIGO INT IDENTITY,
NOME VARCHAR(255),
MAE VARCHAR(255),
PAI VARCHAR(255)
)
CREATE CLUSTERED INDEX PK_CODIGO ON USUARIOS (CODIGO);
CREATE INDEX NOME ON USUARIOS (NOME, CODIGO);
CREATE INDEX MAE ON USUARIOS (MAE, CODIGO) INCLUDE(NOME);
CREATE INDEX PAI ON USUARIOS (PAI, CODIGO) INCLUDE(NOME);
GO
INSERT INTO USUARIOS (NOME, MAE, PAI) VALUES('ADRIANA ARAUJO DIAS','JOAO WILSON ARAUJO','JOSELIA PEREIRA ARAUJO')
INSERT INTO USUARIOS (NOME, MAE, PAI) VALUES('NICOLAS PEDRO SILVA','SERGIO LUIZ SILVA JUNIOR','THAIS BATISTA DOS SANTOS SILVA')
INSERT INTO USUARIOS (NOME, MAE, PAI) VALUES('CARLOS JOSE DOS SANTOS ANJO','ACACIO APARECIDO ANJO DA SILVA','JULIANA ARAUJO DOS SANTOS ANJO')
INSERT INTO USUARIOS (NOME, MAE, PAI) VALUES('NICOLAY RIBEIRO DANTAS','RICARDO VICENTE RIBEIRO','JOZEILDA LUIS ENCARNACAO')
INSERT INTO USUARIOS (NOME, MAE, PAI) VALUES('MARIA DA SILVA MATTOS','FRANKLIN DE OLIVEIRA','BRUNA CATARINA DA SILVA MATTOS')
INSERT INTO USUARIOS (NOME, MAE, PAI) VALUES('CREUSA MARIA MORAES','GELSON DO ESPIRITO SANTO','FATIMA APARECIDA DOS SANTOS')
INSERT INTO USUARIOS (NOME, MAE, PAI) VALUES('DAVID BRADAO BORGES','EMERSON DOS SANTOS BORGES','DAYANE EVANGELISTA ACACIO BORGES')
INSERT INTO USUARIOS (NOME, MAE, PAI) VALUES('LUIZ ADAO SANTOS DA SILVA','WESLEY GONCALVES SILVA','IZABEL BRANDAO DA SILVA')
GO
SELECT CODIGO, NOME, PAI, MAE,
(SELECT COUNT(*) FROM USUARIOS T1
WHERE T1.CODIGO = TAB.CODIGO AND
CHARINDEX(SUBSTRING(NOME, CHARINDEX(' ', NOME)+1, CHARINDEX(' ', NOME, CHARINDEX(' ', NOME)+1) - CHARINDEX(' ', NOME)-1), PAI) > 0 --FUNCIONA PARA O PRIMEIRO SOBRENOME (PAI)
) AS [PAI?],
(SELECT COUNT(*) FROM USUARIOS T1
WHERE T1.CODIGO = TAB.CODIGO AND
CHARINDEX(SUBSTRING(NOME, CHARINDEX(' ', NOME)+1, CHARINDEX(' ', NOME, CHARINDEX(' ', NOME)+1) - CHARINDEX(' ', NOME)-1), MAE) > 0 --FUNCIONA PARA O PRIMEIRO SOBRENOME (MÃE)
) AS [MAE?]
FROM USUARIOS TAB
How far I’ve come, with the above query, I can at least verify with the first surname, but, how to do this query to check all surnames?
Note: The names I presented are fictitious. Do not consider if they happen to coincide with real people.
Maybe using the Full-Text Search feature, it’s more practical
– Jeferson Almeida
If anyone wants to test, here is the fiddle online of the question.
– Randrade
@Randrade Here in the service I do not have access to this site, but thank you, it will help others.
– Ismael
@All you have control over the database or you use some shared hosting?
– Jeferson Almeida
@Jefersonalmeida Total control.
– Ismael
@At night I will write one or a few solutions to your problem
– Jeferson Almeida
@SMAEL: You must have realized that it is necessary to have the mother’s maiden name and, in the case of daughter, if married, her maiden name.
– José Diz
Yes @Josédiz, but in this case, I just want to know if it contains or not. In the future I will delve into this concept, from this.
– Ismael
@Jefersonalmeida How will the solution with Full-Text be? Will it be as simple as the one Randrade posted? Will it perform better?
– Ismael
I created a solution using CLR, that the performance gets better q using XML, shortly put here explaining how to do this
– Jeferson Almeida
@Ismael posted the solution if you have any difficulty can inform me that I further detain the process
– Jeferson Almeida
@Ismael the solutions presented attended you or you need a few more?
– Jeferson Almeida
Hi @Jefersonalmeida, I am focused on another project at this time and still could not see with attention this scenario, although its solution tb has worked.
– Ismael