0

Is it possible to change the value of a column in a check constraint in sql-server? For example I want to be able to test for not NULL like so:

CHECK (column IS NULL)

But change the column to an empty string if this evaluates to true. I'm not an expert in sql-server so thanks for bearing with my ignorance.

1
  • based on your comment, it seems that a before update trigger will do what you need Jan 27, 2015 at 23:28

1 Answer 1

0

A check constraint will restrict any null value from ever entering the table, so you'd get an error thrown before you had the opportunity to convert the value.

What you probably want to do is define your column as not null and add a default constraint to the column

e.g

myColumn varchar(30) not null constraint DF__myColumn default ''
2
  • Ideally this would be the solution but the column currently allows null and I cannot change that constraint easily. I was hoping there was a way to leave the column as allowing nulls but yet change nulls to '' before insert.
    – Tom
    Jan 27, 2015 at 23:15
  • Would a computed column work? something like MyEmptyStringedColumn AS isnull(MyNullableColumn, '').
    – Xedni
    Jan 28, 2015 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.