SQL SERVER has the function ISNULL and function COALESCE which makes your code much more elegant and clean.
That would be your code.
declare @CLIENTES table
(
id int,
Nome varchar(100)
)
INSERT INTO @CLIENTES VALUES
(1, NULL),
(2, 'Jõao')
SELECT id, ISNUll(Nome, 'Meu Nome') as 'Com ISNUll', COALESCE(Nome, 'Meu Nome') as 'com COALESCE'
FROM @CLIENTES
DECLARE @Minha_Var VARCHAR(70)
SELECT @Minha_Var = ISNUll(Nome, 'Meu Nome') FROM @CLIENTES WHERE Id = 1
SELECT @Minha_Var = COALESCE(null, 'Meu Nome') FROM @CLIENTES WHERE Id = 1
SELECT ISNULL(null, 'Meu Nome') AS Using_ISNULL
SELECT COALESCE(null, 'Meu Nome') AS Using_ISNULL
Ricardo, posted working.
– rbz
@Raonibz Thank you, it worked perfectly
– Ricardo Alves
Change for
SET
the variable@Minha_Var
does not assignNULL
if this is the return and you already have a value before. See: When to use SET and SELECT?– Marconi
@Marconi Thanks for the tip. By the way, great didactics in the post reply.
– Ricardo Alves