用sql进行身份证校验代码
时间: 2023-06-04 11:08:38 浏览: 698
可以使用如下的 SQL 语句进行身份证校验:
```sql
SELECT CASE
WHEN LENGTH(idcard) = 15 AND REGEXP_LIKE(idcard, '^[0-9]{15}') THEN
CASE
WHEN TO_CHAR(TO_DATE('19' || SUBSTR(idcard, 7, 6), 'YYYYMMDD'), 'YYYYMMDD') = SUBSTR(idcard, 7, 6) THEN 1
ELSE 0
END
WHEN LENGTH(idcard) = 18 AND REGEXP_LIKE(idcard, '^[0-9]{17}[0-9X]{1}') THEN
CASE
WHEN TO_CHAR(TO_DATE(SUBSTR(idcard, 7, 8), 'YYYYMMDD'), 'YYYYMMDD') = SUBSTR(idcard, 7, 8) AND
SUM(SUBSTR('10X98765432', MOD(SUM(SUBSTR(idcard, 1, 17) * SUBSTR('7913582463', ROWID)), 11) + 1, 1) * POWER(2, ROWID - 1)) MOD 11 =
CASE
WHEN SUBSTR(idcard, 18, 1) = 'X' THEN 10
ELSE CAST(SUBSTR(idcard, 18, 1) AS NUMBER)
END
THEN 1
ELSE 0
END
ELSE 0
END AS is_valid_idcard
FROM dual;
```
其中,idcard 是需要校验的身份证号码。如果返回值为 1,则表示身份证号码校验通过。
阅读全文