regex SQL:找出表1中哪些列值(对于所有行)为空,并在表2中提及所有列值(对于所有行)

q3aa0525  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(106)

假设Table 1(输入数据表)有4个数据行,分别是country、state、city、block。我们只需要检查前三个数据行的Null或空白即可
Table 2(错误表)是Table 1+附加列(称为RejectReason)。对于特定行,此“拒绝原因”应具有所有为空或NULL的列名。例如:“State,City column value is/are null or blank”(顺序或书写风格并不重要,只需说明这两个列值均为null或blank)。
Null或空白表示值= NULL或“”或“”
我的实际工作Table 1有100多万条记录,大约15列(其中7列需要检查是否为Null或空白)。PS:我正在使用SQL Server Management Studio,这是数据验证过程的一部分
那么,做这项工作最快的方法是什么呢?
表1样品Table1 having 4 columns country, state, city, block
表2示例Table2, error table, has 4 + Reject Reason column
我试着分两步做。

INSERT INTO Table2
SELECT A.*,CAST('Mandatory field Blank or NULL: ' AS NVARCHAR(255)) AS 'RejectReason' FROM Table1 AS A 
WHERE Country IS NULL OR Country=''  OR Country=' ' OR
    State IS NULL OR State='' OR State=' ' OR 
    City IS NULL OR City='' OR City=' ' OR

UPDATE Table2
    SET RejectReason = CONCAT(RejectReason, 'Country ')
    WHERE RejectReason like '%Mandatory%' AND (Country IS NULL OR Country ='' OR Country =' ' )
UPDATE Table2
    SET RejectReason = CONCAT(RejectReason, 'State ')
    WHERE RejectReason like '%Mandatory%' AND  (State IS NULL OR State ='' OR State =' ')
UPDATE Table2
    SET RejectReason = CONCAT(RejectReason, 'City ')
    WHERE RejectReason like '%Mandatory%' AND (City IS NULL OR City ='' OR City =' ' )

这首先检查每一行,如果有空或空白。然后逐个检查,哪一列是空或空白(做同样的事情两次)PS:由于table 2中的其他出错行具有不同的RejectReason(如果“列”的整个句子为空或null,则它们不应获得,而仅将“州/市”等词连接到它们:州,城市"连接在一起,这将是最好的。)

wn9m85ua

wn9m85ua1#

您可以在CONCAT_WS函数中使用case表达式,如下所示:

INSERT INTO Table2
SELECT country, state, city,
   CONCAT_WS(' ',
     'Column Null Or Blank:',
     CASE WHEN country IS NULL OR country = '' THEN 'Country' END,
     CASE WHEN state IS NULL OR state = '' THEN 'State' END,
     CASE WHEN city IS NULL OR city = '' THEN 'City' END
   ) RejectReason
FROM table1
WHERE country IS NULL OR state IS NULL OR city IS NULL
 OR '' IN (country, state, city)

请参见demo

相关问题