1

I want to check if the values in a column are in the range of 0 to 255, but the column's type is a char, so I'm trying to use a cast, but I get an error.

This is my code:

alter table poste add constraint chk_poste check ( cast(ad as int )ad_int  (ad_int is not null) and  ad<= 0 and ad>=255);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

0

You are attempting to alias the converted value cast(ad as int )ad_int. This is not valid. Try:

alter table poste add constraint chk_post check (ad ~ '^\d+$' and ad::int between 0 and 255);

It first checks that the initial value numeric (assuming you do not want decimals) and can be converted to the validates the range inclusive of both values.

Belayer
  • 13,578
  • 2
  • 11
  • 22