Set default value for datacolumn in select statement

I want to assign default values ​​to a column in my sql select query so that if the value of this column is null, I get this default value in my recordset. Is there any way to do this?

Example:

select col1 (some default value) from tblname; 
+6
syntax sql-server tsql
source share
3 answers

The preferred way is to use the ANSI-compliant COALESCE function:

 SELECT COALESCE(column_name, default_value) FROM table_name; 

You can also read an article that compares COALESCE and ISNULL .

+16
source share
 select isnull(col1, defaultvalue) from tblname; 
+6
source share

if you use SqlServer you can use the CASE statement

Example:

select register col1 when null and then defaultval else col1 termination with tblname

where defaultval is the default value. The defaultval data type must match the col1 data type.

0
source share

All Articles