[英]Conditional WHERE clause with case statement
I am working with the following query and I'm stuck 我正在使用以下查询,但卡住了
SELECT
@siteID = Site.siteID,
@customerID = Customer.customerID
FROM
Customer
inner join Site on Customer.siteID = Site.siteID
WHERE
Site.phoneNumber = @vaultID
and Customer.clientID = @clientID
Some of the Customer.clientID fields are being updated from a four character field to an eight character field, ex: '1234' is becoming '1234 01'. 部分Customer.clientID字段正从四个字符字段更新为八个字符字段,例如:“ 1234”正变为“ 1234 01”。 The customer will always be passing in a four character ID.
客户将始终输入四个字符的ID。 Depending on what siteID they're record has, I need to modify the query so that it reads only the first four characters of the ID.
根据他们所记录的siteID,我需要修改查询,以使其仅读取ID的前四个字符。 So what I have been trying to work on is adding this in the where clause:
所以我一直在尝试的工作是在where子句中添加它:
AND
CASE
WHEN Site.siteID IN ('1T','1Q')
THEN RTRIM(LTRIM(SUBSTRING(Customer.clientID ,1,6)))= @clientID
ELSE
Customer.clientID = @clientID
END
but I'm not having much luck. 但是我运气不好。 The research I've done so far leads me to believe case statements in the where clause aren't a great idea to begin with but I feel like I'm close here.
到目前为止,我所做的研究使我相信where子句中的案例陈述并不是一个好主意,但我觉得我已经接近了。 I know I can make a totally separate call to the site table before making this call but I really want to get this in one statement, hoping that's possbile.
我知道我可以在进行此调用之前先对站点表进行完全独立的调用,但是我真的很想在一条语句中得到它,希望可以。
The case statement should look like this: case语句应如下所示:
CASE
WHEN Site.siteID IN ('1T','1Q')
THEN RTRIM(LTRIM(SUBSTRING(Customer.clientID ,1,6)))
ELSE Customer.clientID
END = @clientID
You don't do comparisson inside the CASE, rather use the CASE to get you the value which you can compare. 您无需在CASE中进行比较,而可以使用CASE为您提供可以比较的价值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.