tags:

views:

68

answers:

4

I would like to set a default value to a variable

SELECT ID, VALUE
FROM TABLE

Now I would like that value to have a default value of 0 for every ID. What can I do?

+1  A: 

Are you asking how to specify a default value in the select:

SELECT ID, 0 as VALUE FROM TABLE

Or do you want to have the underlying table column have a default value?

Mitch Wheat
i want to set value of a variable to 0
MOHIT BANSAL
@MOHIT BANSAL: won't the `update table set value = 0` would suffice? why need the word DEFAULT there?
Hao
A: 

If you mean you want a default value for when the column is null then you can use the NVL function (at least in oracle), e.g.

SELECT NVL(thecol, 0) FROM thetable;
hamishmcn
`NVL` only works with Oracle, use `COALESCE` to work with other DBs too.
Peter Lang
+1  A: 
select id, 0 as value_default from myTable

so you have an initial default value assigned against a named column (the value of which you can change).

davek
thanks a lot for answer
MOHIT BANSAL
A: 
SELECT ID, IFNULL(VALUE,0) as Value
FROM MyTable

This will select ID, and Value. If value is null it will display 0

TerrorAustralis