为什么我的SQL查询返回的记录与where子句不匹配?

时间:2018-05-19 22:55:41

标签: sql

此查询返回一条记录的结果,其中[E-mail][E-mail 2]均等于'e'。绝对是懒惰推销员的结果。知道为什么会这样吗?

我的查询如下所示,所以我应该只获取电子邮件等于'emailfromsomeone@hotmail.com'正确的记录?

declare @email as varchar
set @email = 'emailfromsomeone@hotmail.com'

select
    C.[No_],
    C.[First Name] firstname, 
    C.[Surname] lastname, 
    C.[E-Mail] as email, 
    C.[E-Mail 2] as email_2, 
    C.[GDPR Opt-in] as GDPR_opt_in,
    C.[Salesperson Code] as sales_person    
from 
    [Contact] as C
where  
    lower(C.[E-Mail]) = lower(@email)
    or lower(C.[E-Mail 2]) = lower(@email)

2 个答案:

答案 0 :(得分:4)

您已宣布@emailVARCHAR,即 VARCHAR。当您设置@email变量时,它会被截断为第一个字符。

尝试将VARCHAR更改为VARCHAR(100)

答案 1 :(得分:-1)

我明白了。所以我声明变量是这样的,然后它工作

declare @email VARCHAR(255)

抱歉打扰你stakoverflow!