2016-03-11 2 views
-3

У меня есть эта строка запроса:SQL WHERE первые 3 буквы НЕ

declare @deadCommunityList table(community varchar(12)) 
    insert into @deadCommunityList (community) values ('000') 
    insert into @deadCommunityList (community) values ('253') 
    insert into @deadCommunityList (community) values ('COU') 
    insert into @deadCommunityList (community) values ('COV') 
    insert into @deadCommunityList (community) values ('D2T') 
    insert into @deadCommunityList (community) values ('D3T') 
    insert into @deadCommunityList (community) values ('DEW') 
    insert into @deadCommunityList (community) values ('DIT') 
    insert into @deadCommunityList (community) values ('E2T') 
    insert into @deadCommunityList (community) values ('E3T') 
    insert into @deadCommunityList (community) values ('EL2') 
    insert into @deadCommunityList (community) values ('EL3') 
    insert into @deadCommunityList (community) values ('ELC') 
    insert into @deadCommunityList (community) values ('ELI') 
    insert into @deadCommunityList (community) values ('ELT') 
    insert into @deadCommunityList (community) values ('ERI') 
    insert into @deadCommunityList (community) values ('FA1') 
    insert into @deadCommunityList (community) values ('GRA') 
    insert into @deadCommunityList (community) values ('GRD') 
    insert into @deadCommunityList (community) values ('GRT') 
    insert into @deadCommunityList (community) values ('HIG') 
    insert into @deadCommunityList (community) values ('HIP') 
    insert into @deadCommunityList (community) values ('LYN') 
    insert into @deadCommunityList (community) values ('NEW') 
    insert into @deadCommunityList (community) values ('PAR') 
    insert into @deadCommunityList (community) values ('PMT') 
    insert into @deadCommunityList (community) values ('RDT') 
    insert into @deadCommunityList (community) values ('RES') 
    insert into @deadCommunityList (community) values ('SCR') 
    insert into @deadCommunityList (community) values ('SCT') 
    insert into @deadCommunityList (community) values ('SMT') 
    insert into @deadCommunityList (community) values ('SUM') 
    insert into @deadCommunityList (community) values ('TB') 
    insert into @deadCommunityList (community) values ('W2T') 
    insert into @deadCommunityList (community) values ('WDV') 
    insert into @deadCommunityList (community) values ('WE2') 
    insert into @deadCommunityList (community) values ('WIC') 
    insert into @deadCommunityList (community) values ('WEC') 
    insert into @deadCommunityList (community) values ('WIL') 
    insert into @deadCommunityList (community) values ('ZIT') 

    SELECT Job_No FROM Schedule WHERE Job_No NOT IN (SELECT * FROM @deadCommunityList) ORDER BY Job_No 

Теперь Job_No будет выглядеть 253DEWT, который не будет соответствовать с 253, что означает строку с Job_No 253DEWT появится, которые я не хочу, чтобы я сделал так, что я пытаюсь сделать, это сопоставить первые 3 символа значениям в deadCommunityList .... какие корректировки я должен сделать?

+8

Священный приговор, Бэтмен. Почему бы тебе не сделать глубокий вдох и повторить это снова. Разделите свой вопрос на правильные предложения, поэтому нам не нужно разбирать этот болезненный беспорядок. –

+0

Я считаю, что вы смотрите подстроку. Вы говорите, что хотите сравнить только 1-го 3 персонажа? – logixologist

+0

253DEWT, который не соответствует 253? Вам действительно нужно вставить 20+ значений, чтобы показать проблему? И вы можете делать значения(),(),() – Paparazzi

ответ

2

Чтобы выбрать первые 3 буквы из колонки:

SELECT LEFT(column_name , 3) FROM table_name; 

В качестве альтернативы вы можете использовать substring функцию:

SELECT SUBSTR(column_name, 1, 3) FROM table_name; 
0

Я считаю, что это то, что вы ищете:

SELECT 
    job_no 
FROM 
    dbo.Schedule S 
WHERE 
    NOT EXISTS 
    (
     SELECT * 
     FROM @deadCommunityList 
     WHERE community = LEFT(S.job_no, 3) 
    ) 
0

все они не все 3

SELECT s.job_no 
    FROM dbo.Schedule S 
WHERE NOT EXISTS (SELECT 1 
         FROM @deadCommunityList 
        WHERE S.job_no like community + '%' 
       ) 
Смежные вопросы