SQL Server:选择字符串不包含某些值的位置

时间:2017-09-17 20:14:50

标签: sql sql-server select

我需要从Department10中选择名称不包含字符串“LA”的员工。

Select *
From EMP
Where Cod_Dept = 10 

2 个答案:

答案 0 :(得分:4)

Select *
From EMP
Where Cod_Dept = 10 
And Name Not Like '%LA%'

答案 1 :(得分:2)

以下是使用CHARINDEX()的另一个选项:

SELECT *
FROM EMP
WHERE Cod_Dept = 10
AND CHARINDEX('LA' , Name , 0) = 0;

如果您有Null值,并且您也希望将其返回:

SELECT *
FROM EMP
WHERE Cod_Dept = 10
AND (CHARINDEX('LA' , Name , 0) = 0) OR (CHARINDEX('LA' , Name , 0) Is Null);

<强> Demo