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.

Tom
  • 3,006
  • 6
  • 33
  • 54

1 Answers1

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 ''
Xedni
  • 3,662
  • 2
  • 16
  • 27
  • 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 '15 at 23:15
  • Would a computed column work? something like MyEmptyStringedColumn AS isnull(MyNullableColumn, ''). – Xedni Jan 28 '15 at 04:16