Question

Hi I have to define a constraint with this case..

    [driver_surcharge_to] [tinyint] NULL CONSTRAINT CK_Vehicle_driverSurchargeTo CHECK (
        1 = case driver_surcharge_from
        when null
            case driver_age_to
                when null then 1
                else 0 end
        else
        case driver_age_to
                when null then 0
                when (driver_age_to) between 16 and 99 then 1
                else 0 end
    end),

I have tried to put the same name field, and dont work with or without it.(brackets)

How can I declare a case to verify his value between two numbers?

Thanks.

Was it helpful?

Solution

You want to use the "condition" form of case:

[driver_surcharge_to] [tinyint] NULL CONSTRAINT CK_Vehicle_driverSurchargeTo CHECK (
    1 = case driver_surcharge_from
    when null
        case driver_age_to
            when null then 1
            else 0 end
    else
    case when driver_age_to is null then 0
         when driver_age_to between 16 and 99 then 1
         else 0 end
end),

I think the following logic is a bit simpler, though:

[driver_surcharge_to] [tinyint] NULL CONSTRAINT CK_Vehicle_driverSurchargeTo CHECK (
    (driver_surcharge_from is null and driver_age_to is null) or
    (driver_surcharge_from is not null and driver_age_to between 16 and 99 )
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top