在SQL—Server中设置一个列的约束只能是八位字符,前两位是0,3-4为数字,第五位是下划线,6-8位是字母。

我主要不会截取,求帮忙
2024-12-25 13:23:26
推荐回答(2个)
回答1:

substring('12345678',1,2)='00'--判断前两位是0
Ascii(substring('12A45678',3,1)) between Ascii('0') and Ascii('9') --判断第三位是数字
Ascii(substring('12A45678',4,1)) between Ascii('0') and Ascii('9') --判断第四位是数字
Ascii(substring('12345678',5,1)) = Ascii('_') --判断第五位是下划线
Ascii(Upper(substring('12345678',6,1))) between Ascii('A') and Ascii('Z') --判断第六位是字母
Ascii(Upper(substring('12345678',7,1))) between Ascii('A') and Ascii('Z')--判断第七位是字母
Ascii(Upper(substring('12345678',8,1))) between Ascii('A') and Ascii('Z')--判断第八位是字母

回答2:

00[0-9][0-9]_[a-z][a-z][a-z]

alter table test
add constraint CK_test check(name like '00[0-9][0-9]_[a-z][a-z][a-z]')