Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a cast a nullable column to a NOT NULL column?

In SQL 2005/8 I'm looking to convert a column in select query to a NOT NULL column.

coalease() and isnull(), although fine functions, is not what I'm looking for. I want the select to throw an error is any of the data is NULL.

Is this possible?

[Update] Clearly the best approach would be to modify the source table, unfortunatly in this case, it's not a (cost-effective) option.

like image 236
Scott Weinstein Avatar asked Dec 17 '22 04:12

Scott Weinstein


1 Answers

What you ask for doesn't exists.

NOT NULL is a constraint. SELECT statements do not enforce constraints. Constraints are only enforced during INSERT/UPDATE/DELETE of the data. If you need to enforce a constraint, declare the constraint on the table, via ALTER TABLE ... ADD...

If you want to return non-null data, use a null translation function like ISNULL or COALESCE.

like image 125
Remus Rusanu Avatar answered Jan 03 '23 20:01

Remus Rusanu