在第一个分号后检查有效值

时间:2016-04-13 08:11:51

标签: sql sql-server

如果可能,任何人都可以帮助我

我正在尝试检查@APPLICATION表的NONVALID列中的数据是否包含remarks_1值 它存在于@ATTRIBUTE表中 我们可以将SKU作为关键列加入表格

DECLARE @ATTRIBUTE TABLE
(
SKU VARCHAR (50),
REMARKS_1 VARCHAR (50)
)

INSERT @ATTRIBUTE


SELECT 'AMRN205-740','P-215-40-18;' UNION ALL
SELECT 'NATN205-740','LT-275-65-20' UNION ALL
SELECT 'NATN205-740','XX-275-65-20' 



DECLARE @APPLICATION TABLE
(
SKU VARCHAR (50),
NONVALID VARCHAR (50)
)

 INSERT @APPLICATION
SELECT 'AMRN205-740','Plus Size; P-215-40-18; 87H' UNION ALL
SELECT 'BCTN205-740','Plus Size; P-215-40-18; 86H' UNION ALL
SELECT 'NATN205-740','Plus Size;  86H' UNION ALL
SELECT 'AMRN205-740', NULL union all
SELECT 'BCTN205-740','P-215-40-18; 86H' UNION ALL
SELECT ''BCTN205-740', ;86H'

预期输出

'NATN205-740','Plus Size;  86H'
'BCTN205-740','P-215-40-18; 86H'-- plus size or semi-plus size before first   semicolon in a string is missing
   BCTN205-740', ;86H'-- plus size or semi-plus size before first semicolon in a string

在第一次SEMICOLON之后,它不包含 LT-275-65-20 XX-275-65-20

非常感谢

1 个答案:

答案 0 :(得分:1)

我建议你更好地规范你的桌子:

我更喜欢名为SKU的SKU列表表格如下:

CREATE TABLE sku
(id int, name varchar(100))

id是PK,name包含您的字段SKU(您的@attribute表)

的内容
CREATE TABLE attribute
(id int, 
 fk_sku int, 
 name varchar(100))

您有属性链接SKU

最后,

CREATE TABLE application
(id int, 
fk_sku int,
fk_attribute int) <-- if you want to store this info or another I don't know

试试这个:

SELECT *
FROM @APPLICATION app
WHERE NOT EXISTS(
    SELECT 'ATTRIBUTE'
    FROM @ATTRIBUTE att
    WHERE att.SKU = app.SKU
    AND PATINDEX('%' + att.REMARKS_1 + '%', app.NONVALID) > 0
)
and app.NONVALID IS NOT NULL

此查询返回&#39; NATN205-740&#39;加上大小; 86H&#39;和&#39; BCTN205-740&#39;,&#39;加上大小; P-215-40-18; 86H&#39;

如果您只想要&#39; NATN205-740&#39;,&#39; Plus Size; 86H&#39;这样:

SELECT *
FROM #APPLICATION app
WHERE EXISTS(
   SELECT 'ATTRIBUTE'
    FROM #ATTRIBUTE att
    WHERE att.SKU = app.SKU
)
AND NOT EXISTS(
    SELECT 'ATTRIBUTE'
    FROM #ATTRIBUTE att
    WHERE att.SKU = app.SKU
    AND PATINDEX('%' + att.REMARKS_1 + '%', app.NONVALID) > 0
)
and app.NONVALID IS NOT NULL