Reputation: 1058
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'. The customer will always be passing in a four character 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. So what I have been trying to work on is adding this in the where clause:
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. 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.
Upvotes: 0
Views: 792
Reputation: 971
The case statement should look like this:
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.
Upvotes: 3