How to change the value of fields in a column?

Asked

Viewed 40 times

4

I am with a simple doubt but I could not find a solution.

The same way I can when I use select bring an empty column using select a.x,NULL as y,a.z from DADOS a, how can I make a column instead of having just values NULL, be filled with some word from my school?

  • Found what you were looking for? Be sure to select the question you think is right

2 answers

5

You can use CASE in the select:

select a.x,
       NULL as y,
       CASE a.z 
           WHEN NULL THEN 'Vazio'
           ELSE a.z
       END as c          

from DADOS a

1

In Mysql you can use the function IFNULL(), As follows:

SELECT IFNULL(UnitsOnOrder,0))
FROM Products

When the Unitsonorder field is null the value will be changed to 0. IFNULL can receive various types of parameters between integers and strings.

If the field is a string where the values can contain both null and empty you can use the expression.

SELECT IF(field1 IS NULL or field1 = '', 'empty', field1) as field1 
from tablename

For other databases there are similar functions that can be seen here.

Browser other questions tagged

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