Hyphenate when value is 0 in select

Asked

Viewed 227 times

1

How can I do in the mysql one query (SELECT) so that when the value is equal to 0, replace it with hífen(-).

Field of value: int.

  • An update? What kind of column?

  • What is the type of field? varchar?

  • A SELECT the field is of the type int

3 answers

2

You can use a IF in consultation.

The IF receives three "parameters", the first is the condition, the second is the value that the column will assume if the condition is met and the third is the value that the column will assume otherwise.

Select If(Campo = 0, '-', Campo) From Tabela 

IF documentation

1


@Eduardo Santos,

If you want to exchange the 0 for the hyphen in a select you do so:

SELECT IF(seu_campo = 0, '-', seu_campo) AS nome_para_esse_resultado FROM sua_tabela;

1

Select IIF(Campo = 0, '-', campo) AS CAMPOSELECIONADO from TABELA

That would work for your case.

Browser other questions tagged

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