Skip to content
Advertisement

SQL query to set CHECK constraint on a column based on the value in other column

I need to set a CHECK constraint on a column based on the value in other column (both within the same table)

Like when COLUMN A is NULL, COLUMN B should be NULL But if COLUMN A has values 1 or 2 or 3 then COLUMN B should definitely have a date value.

Advertisement

Answer

Define B as a date. And then use a check constraint:

If b is a string and you just want it to “look” like a date, then you can do something like:

The date matching isn’t exact, but you haven’t specified the format for the “date” if the column really stores a string.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement