验证记录以查看它是否仅包含数字

时间:2019-05-31 11:22:22

标签: sql sql-server tsql

我正在验证增值税代码。该代码在它们前面包括GB,但我不希望包含它,并查看其余增值税代码是否仅包含数字。

select KUNNR,VAT_CODE,REPLACE(VAT_CODE,'GB',''),
       CASE WHEN REPLACE(VAT_CODE,'GB','') LIKE '%[^0-9]%'   THEN 'Yes' ELSE 'No' END as VAT_MATCH
FROM  CUSTOMER_TBL;

"customer"  "VAT"           "REPLACE(VAT,'GB','')"  "VAT_MATCH"
"0063591544"    "GB111111111"   "111111111"           "No"

1 个答案:

答案 0 :(得分:0)

这就是我要做的:

-- Sample Data
DECLARE @CUSTOMER_TBL TABLE (VAT_CODE VARCHAR(36));
INSERT @CUSTOMER_TBL VALUES ('GB123111GB'),('GB9999111'),('GBB9999111'),('GGB7777222'),
                            ('553399444'),('GB22223366'),('GB22223366X')

-- solution
SELECT t.VAT_CODE, VAT_MATCH = IIF(f.H = 'GB' AND PATINDEX('%[^0-9]%',f.B) = 0,'Yes','No')
FROM        @CUSTOMER_TBL AS t
CROSS APPLY (VALUES (SUBSTRING(t.VAT_CODE,1,2),SUBSTRING(t.VAT_CODE,3,8000))) AS f(H,B);

/* For pre-2012 systems:
   VAT_MATCH = CASE WHEN f.H = 'GB' AND PATINDEX('%[^0-9]%',f.B) = 0 THEN 'Yes' ELSE 'No' END */;

结果

VAT_CODE                             VAT_MATCH
------------------------------------ ---------
GB123111GB                           No
GB9999111                            Yes
GBB9999111                           No
GGB7777222                           No
553399444                            No
GB22223366                           Yes
GB22223366X                          No