How to concatenate a varchar into a Mysql function?

Asked

Viewed 254 times

1

I have this hypothetical function:

BEGIN
DECLARE dado VARCHAR(150);
SET dado = 'teste';
END;

How do I concatenate the variable dado, as an example:

SET dado += ' teste2';

I tried this but it doesn’t work in Mysql.

3 answers

5

You can use the function CONCAT

SELECT CONCAT(primeiro_nome, " ", ultimo_nome) AS Nome_Completo FROM CLIENTE

There’s like configure Mysql to concatenate strings with the operator ||. But I’ve always used the CONCAT for you have always attended to my needs.

I hope I’ve helped.

2

concat(dado,'teste')

that would be it?

1

You can use the Concat both in a SELECT and within a FUNCTION.

Using in the SELECT:

SELECT CONCAT(coluna_1, " STRING ", coluna_2) AS STRING_CONCATENADA FROM SUA_TABELA

Code on Github for future reference

Using in FUNCTION:

CONCAT (string1, string2,…)

In your case, from what I understand, you want for a FUNCTION, so it would look like this:

BEGIN
DECLARE dado  varchar(150);
DECLARE concDado varchar(150);
SET dado = 'teste';
SET concDado = CONCAT(dado , ' teste2');

Code on Github for future reference.

Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.