简体   繁体   English

SQL在子字符串上使用CHECK IN

[英]SQL using CHECK IN on substring

I have added a check constraint on one of my attributes named "CONTACT" as follows : 我对名为“ CONTACT”的属性之一添加了检查约束,如下所示:

CONSTRAINT CONTACTCORRECTNESS  CHECK ( SUBSTRING(CONTACT , 1 , 4) IN    
('0300', '0301', '0302', '0303', '0304', '0305', '0306', '0307', '0308', 
'0309' ,'0341', '0342', '0343', '0344', '0345', '0346', '0347', '0331', '0334', 
'0333', '0334', '0335', '0336', '0321', '0322', '0323', '0324', '0325', 
'0311', '0312', '0313', '0314', '0315')) 

Assume that the contact number is always 11 digits long and is a varchar value , the goal here is to get the first four digits of every new contact entry as it gets fed into the database and verify that either they match one of the string in the set mentioned above(The set of strings from within the check constraint above). 假定联系人号码始终为11位数字,并且是varchar值,此处的目标是获取每个新联系人条目的前四位数字,并将其输入数据库,并验证它们是否与数据库中的字符串之一匹配。上面提到的集合(上面检查约束中的字符串集合)。

I have used the above approach but it does not work as desired even on correct contact numbers a constraint violation occurs. 我已经使用了上述方法,但是即使在正确的联系方式上发生约束冲突,它也无法按需工作。 HELP NEEDED ! 需要帮助 !

use Rtrim and Ltrim inside substring and then try it.... 在子字符串中使用Rtrim和Ltrim,然后尝试...。

CONSTRAINT CONTACTCORRECTNESS  CHECK ( SUBSTRING(LTRIM(RTRIM(CONTACT)) , 1 , 4) IN    
('0300', '0301', '0302', '0303', '0304', '0305', '0306', '0307', '0308', 
'0309' ,'0341', '0342', '0343', '0344', '0345', '0346', '0347', '0331', '0334', 
'0333', '0334', '0335', '0336', '0321', '0322', '0323', '0324', '0325', 
'0311', '0312', '0313', '0314', '0315')) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM